public void doLogin(Connection connection)
        {
            if (connection == null)
                return;

            switch (connection.loginStage)
            {
                case 0: //attempts to login, could also be update server.
                case 2: //login server
                case 4: //login server
                    attemptPlayerLogin(connection);
                    break;
                case 1: //update server [odd numbers]
                case 3: //update server [odd numbers]
                case 5: //update server [odd numbers]
                    updateServer(connection);
                    break;
            }
        }
        public Packets(Player player)
        {
		    this.player = player;
            this.connection = player.getConnection();
	    }
        /**
         * Check and read any incoming bytes.
         * @param p The Player which the frame should be created for.
         * @param forceRead How many bytes to read from the buffer.
         */
        private Packet fillStream(Connection connection, int forceRead)
        {
            if (connection == null)
                return null;

            if (connection.chuckedRawPackets.Count() < forceRead)
                return null;

            PacketBuilder pckt = new PacketBuilder();
            pckt.setSize(Packet.Size.Bare).addBytes(connection.chuckedRawPackets.GetRange(0, forceRead).ToArray(), 0, forceRead);
            connection.chuckedRawPackets.RemoveRange(0, forceRead); //delete read data.
            return pckt.toPacket();
        }
        private void updateServer(Connection connection)
        {
            if (connection == null)
                return;

            try
            {
                if (connection.loginStage == 1)
                {
                    Packet fill_3 = fillStream(connection, 3);
                    if (fill_3 == null) //really is 5, but we guess first 2 could be login server and not updateServer.
                        return;

                    PacketBuilder u1Response = new PacketBuilder();
                    u1Response.setSize(Packet.Size.Bare).addByte((byte)0);
                    connection.SendPacket(u1Response.toPacket());
                    connection.loginStage = 3;
                    updateServer(connection);
                }
                else if (connection.loginStage == 3)
                {
                    Packet fill_8 = fillStream(connection, 8);
                    if (fill_8 == null)
                        return;

                    PacketBuilder ukeys = new PacketBuilder();
                    ukeys.setSize(Packet.Size.Bare).addBytes(misc.UPDATE_KEYS);
                    connection.SendPacket(ukeys.toPacket());
                    connection.loginStage = 5;
                    updateServer(connection);
                }
                else if (connection.loginStage == 5)
                {
                    Packet fill_1 = fillStream(connection, 1);
                    if (fill_1 == null)
                        return;
                    //this is some unknown/not useful packet sent by client useful for quick disconnection.
                    connection.loginStage = 255;
                }
            }
            catch (Exception exception) {
                misc.WriteError(exception.Message); 
            }
        }
        private void attemptPlayerLogin(Connection connection)
        {
            if (connection == null)
                return;

            if (connection.loginStage == 0) //Attempt login or update server.
            {
                Packet fill_2 = fillStream(connection, 2);
                if (fill_2 == null)
                    return;

                int connectionType = fill_2.readByte();

                if (connectionType == 15)
                { //it's update server
                    connection.loginStage = 1;
                    updateServer(connection);
                    return;
                }
                else if (connectionType == 255)
                {
                    connection.SendPacket(new PacketBuilder()
                        .setSize(Packet.Size.Bare)
                        .addBytes(misc.WORLD_LIST_DATA).toPacket());
                    connection.loginStage = 5;
                    updateServer(connection);
                    return;
                }
                else if (connectionType != 14)
                {
                    connection.loginStage = 255; //255 is used as fail.
                    return;
                }

                Random random = new Random();
                long serverSessionKey = ((long)(random.NextDouble() * 99999999D) << 32)
                    + (long)(random.NextDouble() * 99999999D);

                int longPlayerName = fill_2.readByte();

                PacketBuilder s1Response = new PacketBuilder();
                s1Response.setSize(Packet.Size.Bare).addByte((byte)0).addLong(serverSessionKey);
                connection.SendPacket(s1Response.toPacket());
                connection.loginStage = 2;
                attemptPlayerLogin(connection);
            }
            else if (connection.loginStage == 2)
            {
                Packet fill_1 = fillStream(connection, 1);
                if (fill_1 == null)
                    return;

                int loginType = fill_1.readByte();

                if (loginType != 16 && loginType != 18 && loginType != 14)
                {
                    connection.loginStage = 255; //255 is used as fail.
                    return;
                }
                connection.loginStage = 4;
                attemptPlayerLogin(connection);
            }
            else if (connection.loginStage == 4)
            {
                Packet fill_2 = fillStream(connection, 2);
                if (fill_2 == null)
                    return;

                int loginPacketSize = fill_2.readUShort();
                int loginEncryptPacketSize = loginPacketSize - (36 + 1 + 1 + 2);

                if (loginEncryptPacketSize <= 0)
                {
                    connection.loginStage = 255;
                    return;
                }
                Packet fill_loginPacketSize = fillStream(connection, loginPacketSize);
                if (fill_loginPacketSize == null)
                    return;

                int clientVersion = fill_loginPacketSize.readInt();

                if (clientVersion != 530)
                {
                    connection.loginStage = 255;
                    return;
                }

                byte junk1 = fill_loginPacketSize.readByte();
                byte lowMem = fill_loginPacketSize.readByte(); //0 is this still low mem ver?
                byte zero = fill_loginPacketSize.readByte();
                byte b1 = fill_loginPacketSize.readByte();
                ushort s1 = fill_loginPacketSize.readUShort();
                ushort s2 = fill_loginPacketSize.readUShort();
                byte b2 = fill_loginPacketSize.readByte(); 

                for (int i = 0; i < 24; i++)
                {
                    int cacheIDX = fill_loginPacketSize.readByte();
                }
                string appletSettings = fill_loginPacketSize.readRS2String(); //EkKmok3kJqOeN6D3mDdihco3oPeYN2KFy6W5--vZUbNA
                int someInt1 = fill_loginPacketSize.readInt();
                int someInt2 = fill_loginPacketSize.readInt();
                ushort short1 = fill_loginPacketSize.readUShort();

                for (int i = 0; i < 28; i++)
                {
                    int crcOfClientClasses = fill_loginPacketSize.readInt();
                }

                int junk2 = fill_loginPacketSize.readByte();
                int encryption = fill_loginPacketSize.readByte();

                if (encryption != 10 && encryption != 64)
                {
                    connection.loginStage = 255;
                    return;
                }

                long clientSessionKey = fill_loginPacketSize.readLong();
                long serverSessionKey = fill_loginPacketSize.readLong();
                LoginDetails loginDetails = new LoginDetails();
                loginDetails.setLongName(fill_loginPacketSize.readLong()); //must start a 225.
                loginDetails.setUsername(misc.longToPlayerName(loginDetails.getLongName()).ToLower().Replace("_", " ").Trim());
                loginDetails.setPassword(fill_loginPacketSize.readRS2String());

                Console.WriteLine("Attempting to login with Username: "******" Password: " + loginDetails.getPassword());
                connection.setLoginDetails(loginDetails);
                //start attempting to login the account.
                lock (playersToLoad)
                {
                    playersToLoad.Enqueue(connection);
                }

                connection.loginStage = 6;
            }

        }
        /**
         * Checks if the Player is the update checker and returns true is update was sent.
         * Used to disconnect sockets faster then 15 timeout disconnect.
         * @param p The Player which the frame should be created for.
         */
        public static bool removableConnection(Connection connection)
        {
            if (connection == null || connection.socket == null || !connection.socket.Connected)
                return true;

            return connection.loginStage == 255;
        }
        public ReturnCode loadPlayer(Connection connection)
        {

            if (connection == null)
                return ReturnCode.COULD_NOT_COMPLETE;
            LoginDetails loginDetails = connection.getLoginDetails();
            if (loginDetails == null || loginDetails.getUsername() == "" || loginDetails.getLongName() == 0)
                return ReturnCode.INVALID_PASSWORD;//ReturnCode.INVALID_PASSWORD;

            foreach (char c in loginDetails.getUsername().ToCharArray())
            {
                if (!char.IsLetterOrDigit(c) && !char.IsWhiteSpace(c))
                    return ReturnCode.INVALID_PASSWORD;
            }
            Player createdPlayer = new Player(connection);
            connection.setPlayer(createdPlayer); //player finally created.
            createdPlayer.setLoginDetails(loginDetails);

            if (!File.Exists(misc.getServerPath() + @"\accounts\" +  loginDetails.getUsername() + ".xml")) {

                createdPlayer.setRights(2); //all new users admins atm (change later).
                createdPlayer.setLocation(new Location(2323, 3174, 0));
                return ReturnCode.LOGIN_OK; //new user.
            }
            //Yeah reading XML files is a bit h**o.
            try
            {
                int temp;
                long lTemp;
                double dTemp;
                string username = createdPlayer.getLoginDetails().getUsername().ToLower();
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(misc.getServerPath() + @"\accounts\" + username + ".xml");

                XmlNode xmlNode = xmlDocument.SelectSingleNode("/Player/Login/Password");
                if (xmlNode == null) return ReturnCode.INVALID_PASSWORD; //no password node.
                if (createdPlayer.getLoginDetails().getPassword() != xmlNode.InnerText)
                    return ReturnCode.INVALID_PASSWORD;

                XmlNode loginElement = xmlDocument.SelectSingleNode("/Player/Login");
                if(loginElement != null && loginElement.HasChildNodes) {
                    XmlNodeList childs = loginElement.ChildNodes;

                    foreach (XmlElement element in childs)
                    {
                        switch (element.Name)
                        {
                            case "Rights":
                                if (!int.TryParse(element.InnerText, out temp))
                                    temp = 0;
                                createdPlayer.setRights(temp);
                                break;
                            case "BankPin":
                                if (element.InnerText == "") continue;
                                createdPlayer.getBank().setBankPin(element.InnerText);
                                break;
                        }
                    }
                }

                loginElement = xmlDocument.SelectSingleNode("/Player/Position");
                if (loginElement != null && loginElement.HasChildNodes)
                {
                    XmlNodeList childs = loginElement.ChildNodes;
                    Location location = new Location();
                    foreach (XmlElement element in childs)
                    {
                        switch (element.Name)
                        {
                            case "X":
                                if (!int.TryParse(element.InnerText, out temp)) {
                                    createdPlayer.setLocation(Constants.HOME_SPAWN_LOCATION.randomSpawn());
                                    break;
                                }
                                location.setX(temp);
                                break;
                            case "Y":
                                if (!int.TryParse(element.InnerText, out temp)) {
                                    createdPlayer.setLocation(Constants.HOME_SPAWN_LOCATION.randomSpawn());
                                    break;
                                }
                                location.setY(temp);
                                break;
                            case "Z":
                                if (!int.TryParse(element.InnerText, out temp))
                                    temp = 0;
                                location.setZ(temp);
                                break;
                        }
                    }
                    createdPlayer.setLocation(location);
                    if (Location.atDuelArena(createdPlayer.getLocation()))
                        DuelSession.teleportDuelArenaHome(createdPlayer);
                }

                xmlNode = xmlDocument.SelectSingleNode("/Player/Settings/RunEnergy");
                if (xmlNode == null)
                {
                    temp = 100;
                } else {
                    if (!int.TryParse(xmlNode.InnerText, out temp))
                        temp = 100;
                }
                createdPlayer.setRunEnergyLoad(temp);

                loginElement = xmlDocument.SelectSingleNode("/Player/Settings/PrivacySettings");
                if (loginElement != null && loginElement.HasChildNodes)
                {
                    XmlNodeList childs = loginElement.ChildNodes;
                    Friends.STATUS publicStatus = Friends.STATUS.ON, privateStatus = Friends.STATUS.ON, tradeStatus = Friends.STATUS.ON;

                    foreach (XmlElement element in childs)
                    {
                        switch (element.Name)
                        {
                            case "Public":
                                publicStatus = (Friends.STATUS)Enum.Parse(typeof(Friends.STATUS), element.InnerText, true);
                                break;
                            case "Private":
                                privateStatus = (Friends.STATUS)Enum.Parse(typeof(Friends.STATUS), element.InnerText, true);
                                break;
                            case "Trade":
                                tradeStatus = (Friends.STATUS) Enum.Parse(typeof(Friends.STATUS), element.InnerText, true);
                                break;
                        }
                    }
                    createdPlayer.getFriends().setPrivacyOptions(publicStatus, privateStatus, tradeStatus);
                }

                loginElement = xmlDocument.SelectSingleNode("/Player/Friends");
                if (loginElement != null && loginElement.HasChildNodes)
                {
                    XmlNodeList childs = loginElement.ChildNodes; //number of Friends

                    foreach (XmlElement element in childs)
                    {
                        if(element.Name == "Friend") {
                            if(long.TryParse(element.InnerText, out lTemp))
                                createdPlayer.getFriends().getFriendsList().Add(lTemp);
                        }
                    }
                }

                loginElement = xmlDocument.SelectSingleNode("/Player/Ignores");
                if (loginElement != null && loginElement.HasChildNodes)
                {
                    XmlNodeList childs = loginElement.ChildNodes; //number of Friends

                    foreach (XmlElement element in childs)
                    {
                        if (element.Name == "Ignore") {
                            if (long.TryParse(element.InnerText, out lTemp))
                                createdPlayer.getFriends().getIgnoresList().Add(lTemp);
                        }
                    }
                }

                loginElement = xmlDocument.SelectSingleNode("/Player/Stats");
                if (loginElement != null && loginElement.HasChildNodes)
                {
                    XmlNode skillNode;
                    foreach (Skills.SKILL skill in Enum.GetValues(typeof(Skills.SKILL)))
                    {
                        skillNode = loginElement.SelectSingleNode("/Player/Stats/" + skill.ToString());

                        foreach (XmlElement element in skillNode.ChildNodes)
                        { //CurrentLevel/XP
                            switch (element.Name)
                            {
                                case "CurrentLevel":
                                    if (!int.TryParse(element.InnerText, out temp))
                                    {
                                        if (skill != Skills.SKILL.HITPOINTS)
                                            createdPlayer.getSkills().setCurLevel(skill, 1);
                                        else
                                            createdPlayer.getSkills().setCurLevel(skill, 10);
                                        break;
                                    }
                                    createdPlayer.getSkills().setCurLevel(skill, temp);
                                    break;
                                case "XP":
                                    if (!Double.TryParse(element.InnerText, out dTemp))
                                    {
                                        if (skill != Skills.SKILL.HITPOINTS)
                                            createdPlayer.getSkills().setXp(skill, 0);
                                        else
                                            createdPlayer.getSkills().setXp(skill, 1184);
                                        break;
                                    }
                                    createdPlayer.getSkills().setXp(skill, dTemp);
                                    break;
                            }
                        }
                    }
                }

                loginElement = xmlDocument.SelectSingleNode("/Player/EquipmentItems");
                if (loginElement != null && loginElement.HasChildNodes)
                {
                    XmlNode skillNode;

                    foreach (ItemData.EQUIP equip in Enum.GetValues(typeof(ItemData.EQUIP))) {
                        if (equip == ItemData.EQUIP.NOTHING) continue;
                        skillNode = loginElement.SelectSingleNode("/Player/EquipmentItems/" + equip.ToString());
                        if (skillNode == null) continue;
                        int id = -1, amount = 0;
                        foreach (XmlElement element in skillNode.ChildNodes)
                        {
                            switch (element.Name)
                            {
                                case "Id":
                                    if (!int.TryParse(element.InnerText, out id))
                                        id = -1;
                                    break;
                                case "Amount":
                                    if (!int.TryParse(element.InnerText, out amount))
                                        amount = 0;
                                    break;
                            }
                        }
                        if (id != -1) {
                            createdPlayer.getEquipment().getEquipment()[(int)equip].setItemId(id);
                            createdPlayer.getEquipment().getEquipment()[(int)equip].setItemAmount(amount);
                        }
                    }
                }

                loginElement = xmlDocument.SelectSingleNode("/Player/InventoryItems");
                if (loginElement != null && loginElement.HasChildNodes)
                {
                    int slot = -1, id = 0, amount = 0;
                    foreach (XmlElement itemElement in loginElement.ChildNodes) //each item.
                    {
                        foreach (XmlElement itemDef in itemElement.ChildNodes) //each item.
                        {
                            switch (itemDef.Name)
                            {
                                case "Slot":
                                    if (!int.TryParse(itemDef.InnerText, out slot))
                                        slot = -1;
                                    if (slot < 0 || slot > Inventory.MAX_INVENTORY_SLOTS) slot = -1;
                                    break;
                                case "Id":
                                    if (!int.TryParse(itemDef.InnerText, out id))
                                        slot = -1;
                                    break;
                                case "Amount":
                                    if (!int.TryParse(itemDef.InnerText, out amount))
                                        slot = -1;
                                    break;
                            }
                        }
                        if (slot != -1) {
                            createdPlayer.getInventory().getItems()[slot].setItemId(id);
                            createdPlayer.getInventory().getItems()[slot].setItemAmount(amount);
                        }
                    }

                }

                loginElement = xmlDocument.SelectSingleNode("/Player/BankItems");
                if (loginElement != null && loginElement.HasChildNodes)
                {
                    int slot = -1, id = 0, amount = 0;
                    foreach (XmlElement itemElement in loginElement.ChildNodes) //each item.
                    {
                        foreach (XmlElement itemDef in itemElement.ChildNodes) //each item.
                        {
                            switch (itemDef.Name)
                            {
                                case "Slot":
                                    if (!int.TryParse(itemDef.InnerText, out slot))
                                        slot = -1;
                                    if (slot < 0 || slot > Inventory.MAX_INVENTORY_SLOTS) slot = -1;
                                    break;
                                case "Id":
                                    if (!int.TryParse(itemDef.InnerText, out id))
                                        slot = -1;
                                    break;
                                case "Amount":
                                    if (!int.TryParse(itemDef.InnerText, out amount))
                                        slot = -1;
                                    break;
                            }
                        }
                        if (slot != -1)
                        {
                            createdPlayer.getBank().getBank()[slot].setItemId(id);
                            createdPlayer.getBank().getBank()[slot].setItemAmount(amount);
                        }
                    }
                }

                return ReturnCode.LOGIN_OK; //new user.
            }
            catch (Exception e)
            {
                misc.WriteError(e.Message);
                return ReturnCode.COULD_NOT_COMPLETE;
            }
        }