public static PacketProcessResult HandleClient(Packet pck, RelaySession session)
        {
            byte locale = pck.ReadUInt8();
            string username = pck.ReadAscii();
            string password = pck.ReadAscii();
            ushort ServerID = pck.ReadUInt16();
            //проверить
            if (Global.EnableIpAccountLimitation)
            {

                int nResult = Global.dbmgr.GetIpLimitationResult(username, Utility.GetRemoteEpString(session.Arguments.ClientSocket));
                if (nResult == 0)
                {
                    Packet login_response = new Packet(0xA102, false);
                    login_response.WriteUInt8(0x02);
                    login_response.WriteUInt8(12);
                    session.SendPacketToClient(login_response);
                    //drop conn
                    return PacketProcessResult.Disconnect;
                }
            }
            if (Global.EnableLoginProcessing)
            {

                Packet login = new Packet(0x6102);
                login.WriteUInt8(Global.OriginalLocale > 0 && locale > 0 ? Global.OriginalLocale : locale);
                login.WriteAscii(username);
                login.WriteAscii(Global.EnableUseSha1Salt ? Utility.HashPassword(username, password) : password);
                login.WriteUInt16(ServerID);
                session.SendPacketToModule(login);
                //return PacketProcessResult.DoNothing;
                return PacketProcessResult.ContinueLoop;
            }
            return PacketProcessResult.DoNothing;
        }
        public static PacketProcessResult HandleClient(Packet pck, RelaySession session, SilkroadServer server)
        {
            byte locale = pck.ReadUInt8();
            string username = pck.ReadAscii();
            string password = pck.ReadAscii();
            ushort ServerID = pck.ReadUInt16();

            if (Global.EnableServerInspection && !Global.InspectionLoginIgnore.Contains(username))
            {
                Packet login_response = new Packet(0xA102, false);
                login_response.WriteUInt8(0x02);
                login_response.WriteUInt8(0x02);
                login_response.WriteUInt8(0x02);
                session.SendPacketToClient(login_response);

                return PacketProcessResult.Disconnect;
            }

            if (Global.EnableIpAccountLimitation)
            {
                //We use Utility.GetRemoteEpString because username is assigned after AgentServer.UserAuth
                string clientAddr = Utility.GetRemoteEpString(session.Arguments.ClientSocket);
                int connectionCount = Global.srvmgr.GetUserCountByIpForAgent(clientAddr);

                if (connectionCount >= Global.AccountIpLimitCount)
                {
                    Packet login_resp = new Packet(0xA102, false);
                    login_resp.WriteUInt8(0x02);
                    login_resp.WriteUInt8(12);
                    session.SendPacketToClient(login_resp);

                    return PacketProcessResult.Disconnect;
                }
            }

            int serverOnline = 0;
            int.TryParse(session.State["server_" + ServerID] as string, out serverOnline);
            if (Global.ShardMaxOnline > 0 && Global.ShardMaxOnline <= serverOnline)
            {
                Packet login_response = new Packet(0xA102, false);
                login_response.WriteUInt8(0x02);
                login_response.WriteUInt8(5);
                session.SendPacketToClient(login_response);
                return PacketProcessResult.Disconnect;
            }

            if (Global.EnableLoginProcessing)
            {

                Packet login = new Packet(0x6102);
                login.WriteUInt8(Global.OriginalLocale > 0 && locale > 0 ? Global.OriginalLocale : locale);
                login.WriteAscii(username);
                login.WriteAscii(Global.EnableUseSha1Salt ? Utility.HashPassword(username, password) : password);
                login.WriteUInt16(ServerID);
                session.SendPacketToModule(login);
                //return PacketProcessResult.DoNothing;
                return PacketProcessResult.ContinueLoop;
            }
            return PacketProcessResult.DoNothing;
        }
        public static PacketProcessResult HandleModule(Packet pck, RelaySession session, SilkroadServer server)
        {
            Packet ServerList = new Packet(0xA101);
            byte GlobalOperationFlag = pck.ReadUInt8();

            ServerList.WriteUInt8(GlobalOperationFlag);

            while (GlobalOperationFlag == 1)
            {
                byte GlobalOperationType = pck.ReadUInt8();
                string GlobalOperationName = pck.ReadAscii();

                GlobalOperationFlag = pck.ReadUInt8();
                ServerList.WriteUInt8(GlobalOperationType);
                ServerList.WriteAscii(GlobalOperationName);
                ServerList.WriteUInt8(GlobalOperationFlag);
            }
            byte ShardFlag = pck.ReadUInt8();

            ServerList.WriteUInt8(ShardFlag);

            while (ShardFlag == 1)
            {
                uint ShardID = pck.ReadUInt16();
                string ShardName = pck.ReadAscii();
                uint ShardCurrent = pck.ReadUInt16();
                uint ShardCapacity = pck.ReadUInt16();
                byte ShardStatus = pck.ReadUInt8();
                byte GlobalOperationID = pck.ReadUInt8();
                ShardFlag = pck.ReadUInt8();
                session.State["server_" + ShardID] = ShardCurrent;

                ServerList.WriteUInt16(ShardID);
                ServerList.WriteAscii("Возрождение", Global.TextEncodeCode);
                ServerList.WriteUInt16(ShardCurrent + Global.ShardFakeOnline);
                ServerList.WriteUInt16(Global.ShardMaxOnline > 0 ? (uint)Global.ShardMaxOnline : ShardCapacity);
                ServerList.WriteUInt8(ShardStatus);
                ServerList.WriteUInt8(GlobalOperationID);
                ServerList.WriteUInt8(ShardFlag);

            }
            session.SendPacketToClient(ServerList);
            return PacketProcessResult.ContinueLoop;
        }
        public static PacketProcessResult HandleModule(Packet pck, RelaySession session, SilkroadServer server)
        {
            if (server.HasAgentRedirectRules())
            {
                string src_host;
                int src_port;
                byte flag1 = pck.ReadUInt8();

                if (flag1 == 1)
                {
                    UInt32 uint1 = pck.ReadUInt32();
                    src_host = pck.ReadAscii();
                    src_port = pck.ReadUInt16();

                    bool redirectRuleFound = false;
                    for (int j = 0; j < server.RedirectRules.Count; j++)
                    {

                        if (server.RedirectRules[j].OriginalIp == src_host && server.RedirectRules[j].OriginalPort == src_port)
                        {
                            Packet mypck = new Packet(0xA102, false);
                            mypck.WriteUInt8(flag1);
                            mypck.WriteUInt32(uint1);
                            mypck.WriteAscii(server.RedirectRules[j].NewIp);
                            mypck.WriteUInt16((ushort)server.RedirectRules[j].NewPort);

                            //  m_ClientSecurity.Send(mypck);
                            session.SendPacketToClient(mypck);
                            redirectRuleFound = true;

                            break;

                        }

                    }
                    if (!redirectRuleFound)
                    {
                        Global.logmgr.WriteLog(LogLevel.Warning, "Agent redirect rules given, but [{0}:{1}] is unknown agent server", src_host, src_port);
                    }
                    else
                    {
                        return PacketProcessResult.ContinueLoop;
                    }

                }

            }
            return PacketProcessResult.DoNothing;
        }
        public static PacketProcessResult HandleClient(Packet pck, RelaySession session, SilkroadServer server)
        {
            UInt32 uint32_1 = pck.ReadUInt32();
            string uname = pck.ReadAscii();
            string passw = pck.ReadAscii();
            byte locale = pck.ReadUInt8();
            uint ukn1 = pck.ReadUInt32();
            uint ukn = pck.ReadUInt16();
            if (uname.Contains("'"))
            {
                Global.logmgr.WriteLog(LogLevel.Error, "User trying to exploit username: {0}", uname);
                return PacketProcessResult.Disconnect;
            }
            //инициализурем данные
            session.State["username"] = uname;
            session.State["isBot"] = false;
            session.State["isSafe"] = false;
            session.State["noticeDone"] = false;
            session.State["lastExchange"] = DateTime.Now;
            session.State["lastLogOut"] = DateTime.Now;
            session.State["lastStall"] = DateTime.Now;
            session.State["proper_logout"] = false;
            session.State["level"] = 0;
            //пишем в базу что юзер залогинился
            Global.dbmgr.AnticheatAuthLog(uname, session.State["ip_address"] as string);

            Global.logmgr.WriteLog(LogLevel.Notify, "User logged in [{0}]", uname);

            if (Global.EnableBotDetected && locale == Global.OriginalLocale)
            {
                session.State["isBot"] = true;
            }
            if (Global.EnableLoginProcessing)
            {
                Packet p = new Packet(0x6103);
                p.WriteUInt32(uint32_1);
                p.WriteAscii(uname);
                p.WriteAscii(Global.EnableUseSha1Salt ? Utility.HashPassword(uname, passw) : passw);
                p.WriteUInt8(Global.OriginalLocale > 0 && locale > 0 ? Global.OriginalLocale : locale);
                p.WriteUInt32(ukn1);
                p.WriteUInt16(ukn);
                // m_ModuleSecurity.Send(p);
                session.SendPacketToModule(p);
                return PacketProcessResult.ContinueLoop;
            }
            return PacketProcessResult.DoNothing;
        }
        public static PacketProcessResult HandleClient(Packet pck, RelaySession session, SilkroadServer server)
        {
            if (Global.EnableGmAccessControl)
            {
                //Делаем запрос в базу и проверяем разрешеная ли команда
                uint commandID = pck.ReadUInt16();

                string uname = session.State["username"] as string;
                if (Global.dbmgr.checkGmAccessControl(uname, commandID) == 1)
                {

                    if (commandID == 6 || commandID == 7)
                    {
                        //Делаем проверку на разрешение использовать этот ID
                        uint obj_id = pck.ReadUInt32();
                        uint amountOrOptLevel = pck.ReadUInt8();

                        if (Global.dbmgr.checkGmObjAccessControl(uname, obj_id, amountOrOptLevel) == 0)
                        {
                            session.SendClientNotice("UIIT_STT_ANTICHEAT_GM_USE_CREATE");
                            return PacketProcessResult.ContinueLoop;

                        }
                    }
                    //Убираем возможность полноценно килять мобов гму.
                    if (commandID == 20)
                    {
                        uint k_mob_id = pck.ReadUInt32();
                        Packet p = new Packet(0x7010);
                        p.WriteUInt16(20);
                        p.WriteUInt32(k_mob_id);
                        p.WriteUInt8(1);
                        session.SendPacketToModule(p);
                        return PacketProcessResult.ContinueLoop;
                    }
                }
                else
                {
                    session.SendClientNotice("UIIT_STT_ANTICHEAT_GM_USE_COMMAND");
                    return PacketProcessResult.ContinueLoop;
                }
            }

            return PacketProcessResult.DoNothing;
        }
        public static PacketProcessResult HandleModule(Packet pck, RelaySession session, SilkroadServer server)
        {
            uint type = pck.ReadUInt16();
            if (Global.EnableUniqueDeathNotify && type == 3078)
            {
                uint mob_id = pck.ReadUInt32();
                string char_name = pck.ReadAscii();
                //Проверяем тормоз, дабы с каждого потока не спамило
                if(Global.UniqueDeathNotifyName == char_name && Global.UniqueDeathNotifyID == mob_id && Global.UniqueDeathNotifyTime > DateTime.Now)
                {
                    return PacketProcessResult.DoNothing;
                }

                Global.UniqueDeathNotifyName = char_name;
                Global.UniqueDeathNotifyID = mob_id;
                Global.UniqueDeathNotifyTime = DateTime.Now.AddSeconds(10);
                Global.dbmgr.UniqueDeathNotify(char_name, (int)mob_id);
            }

            return PacketProcessResult.DoNothing;
        }
        public static PacketProcessResult HandleModule(Packet pck, RelaySession session, SilkroadServer server)
        {
            bool isBot = false;
            bool.TryParse(session.State["isBot"] as string, out isBot);
            if (Global.UseSafeRegion && isBot)
            {
                pck.ReadUInt32();
                pck.ReadUInt8();
                int region = pck.ReadUInt16();

                if (Global.SafeRegions.Contains(region))
                {
                    session.State["isSafe"] = true;
                }
                else
                {
                    session.State["isSafe"] = false;
                }
            }

            return PacketProcessResult.DoNothing;
        }
        private bool ServerPacketHandler(Context context, Packet packet)
        {
            bool retval = false;
            switch (packet.Opcode)
            {
                case 0x303D:
                    {
                        packet.SeekRead(24, SeekOrigin.Begin);
                        uint maxHP = packet.ReadUInt32();
                        uint maxMP = packet.ReadUInt32();
                        _charracter.MaxHP = maxHP;
                        _charracter.MaxMP = maxMP;
                        if (_charracter.CurrentHP > 0)
                        {
                            _mainForm.UpdateProgressBarValue(_charracter.MaxHP, _charracter.MaxHP, _charracter.MaxMP, _charracter.MaxMP);
                        }

                    }
                    break;
                case 0x3020:
                    {
                        _charracter.Id = packet.ReadUInt32();
                        Packet packet3013 = _charracter.Opcode3013;
                        string id = BitConverter.ToString(BitConverter.GetBytes(_charracter.Id)).Replace("-", "");
                        string tempPacket = BitConverter.ToString(packet3013.GetBytes()).Replace("-", "");
                        int index = tempPacket.IndexOf(id, 0);

                        if (index > 0)
                        {
                            float X = 0, Y = 0;
                            byte XX = 0, YY = 0;
                            index = index / 2 + 4;
                            packet3013.SeekRead(index, SeekOrigin.Begin);
                            ushort zonetype = packet3013.ReadUInt16();
                            XX = BitConverter.GetBytes(zonetype)[0];
                            YY = BitConverter.GetBytes(zonetype)[1];
                            if (zonetype >= 32769 && zonetype <= 32775)
                            {
                                X = packet3013.ReadSingle();
                                packet3013.SeekRead(index + 14, SeekOrigin.Begin);
                                Y = packet3013.ReadSingle();
                            }
                            else
                            {
                                X = packet3013.ReadSingle();
                                packet3013.SeekRead(index + 10, SeekOrigin.Begin);
                                Y = packet3013.ReadSingle();
                            }
                            _charracter.XCoord = (int)Math.Floor((XX - 135) * 192 + X / 10);
                            _charracter.YCoord = (int)Math.Floor((YY - 92) * 192 + Y / 10);
                            _mainForm.UpdateCoord("X : " + _charracter.XCoord + "   " + "Y : " + _charracter.YCoord);
                        }

                        packet3013.Dispose();
                        _charracter.Opcode3013 = null;
                    }
                    break;
                case 0x3057:
                    {
                        uint tempID = packet.ReadUInt32();
                        packet.SeekRead(6, SeekOrigin.Begin);

                        if (tempID == _charracter.Id)
                        {
                            byte check = packet.ReadUInt8();
                            if (check == 1)
                            {
                                _charracter.CurrentHP = packet.ReadUInt32();
                            }
                            else if (check == 2)
                            {
                                _charracter.CurrentMP = packet.ReadUInt32();
                            }
                        }
                        _mainForm.UpdateProgressBarValue(_charracter.MaxHP, _charracter.CurrentHP, _charracter.MaxMP, _charracter.CurrentMP);
                    }
                    break;
                case 0x3013:
                    {
                        //Print(packet, "S->P");
                        _charracter.Opcode3013 = packet;
                        packet.SeekRead(42, SeekOrigin.Begin);
                        _charracter.CurrentHP = packet.ReadUInt32();
                        _charracter.CurrentMP = packet.ReadUInt32();
                        //_mainForm.UpdateProgressBarValue(_charracter.MaxHP, _charracter.CurrentHP, _charracter.MaxMP, _charracter.CurrentMP);
                    }
                    break;
                case 0xB021:
                    {
                        byte XX = 0, YY = 0;
                        ushort X = 0, Y = 0;
                        uint uintObjectID = packet.ReadUInt32();
                        byte byteDes = packet.ReadUInt8();
                        XX = packet.ReadUInt8();
                        YY = packet.ReadUInt8();
                        packet.SeekRead(5, SeekOrigin.Begin);
                        ushort zonetype = packet.ReadUInt16();
                        packet.SeekRead(7, SeekOrigin.Begin);
                        if (byteDes != 0)
                        {
                            if (uintObjectID == _charracter.Id)
                            {
                                if (zonetype >= 32769 && zonetype <= 32775)
                                {
                                    X = (ushort)(packet.ReadUInt16() - packet.ReadUInt16());
                                    packet.SeekRead(15, SeekOrigin.Begin);
                                    Y = (ushort)(packet.ReadUInt16() - packet.ReadUInt16());
                                }
                                else
                                {
                                    X = packet.ReadUInt16();
                                    packet.ReadUInt16();
                                    Y = packet.ReadUInt16();
                                }
                                _charracter.XCoord = (int)Math.Floor((XX - 135) * 192 + X / 10.0);
                                _charracter.YCoord = (int)Math.Floor((YY - 92) * 192 + Y / 10.0);

                                _mainForm.UpdateCoord("X : " + _charracter.XCoord + "   " + "Y : " + _charracter.YCoord);
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
            return retval;
        }
        /// <summary>
        /// DownloadServer info / patch info request packet (Module -> Client)
        /// </summary>
        /// <param name="pck"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public static PacketProcessResult HandleModule(Packet pck, RelaySession session, SilkroadServer server)
        {
            if (server.HasDownloadRedirectRules())
            {
                //Read original data
                byte result = pck.ReadUInt8();

                //UpdateInfo
                if (result == 0x02)
                {
                    byte errorCode = pck.ReadUInt8();

                    if (errorCode == 0x02)
                    {
                        Packet myPacket = new Packet(0xA100, false, true);

                        string tmpIp = pck.ReadAscii();
                        ushort tmpPort = pck.ReadUInt16();

                        bool redirectRuleFound = false;
                        for (int i = 0; i < server.RedirectRules.Count; i++)
                        {
                            if (server.RedirectRules[i].OriginalIp == tmpIp && server.RedirectRules[i].OriginalPort == tmpPort)
                            {
                                tmpIp = server.RedirectRules[i].NewIp;
                                tmpPort = (ushort)server.RedirectRules[i].NewPort;
                                redirectRuleFound = true;
                            }
                        }

                        if (!redirectRuleFound)
                        {
                            Global.logmgr.WriteLog(LogLevel.Warning, "Download redirect rules given, but [{0}:{1}] is unknown download server", tmpIp, tmpPort);
                        }

                        uint version = pck.ReadUInt32();
                        byte fileFlag = pck.ReadUInt8();

                        myPacket.WriteUInt8(result);
                        myPacket.WriteUInt8(errorCode);

                        myPacket.WriteAscii(tmpIp);
                        myPacket.WriteUInt16(tmpPort);
                        myPacket.WriteUInt32(version);
                        myPacket.WriteUInt8(fileFlag);

                        while (fileFlag == 0x01)
                        {
                            uint fileId = pck.ReadUInt32();
                            string fileName = pck.ReadAscii();
                            string filePath = pck.ReadAscii();
                            uint fileLength = pck.ReadUInt32();
                            byte doPack = pck.ReadUInt8();

                            fileFlag = pck.ReadUInt8();

                            myPacket.WriteUInt32(fileId);
                            myPacket.WriteAscii(fileName);
                            myPacket.WriteAscii(filePath);
                            myPacket.WriteUInt32(fileLength);
                            myPacket.WriteUInt8(doPack);
                            myPacket.WriteUInt8(fileFlag);
                        }
                        session.SendPacketToClient(myPacket);
                    }
                    //Do not send original packet
                    return PacketProcessResult.ContinueLoop;
                }
            }
            return PacketProcessResult.DoNothing;
        }
        public static PacketProcessResult HandleClient(Packet pck, RelaySession session, SilkroadServer server)
        {
            if (Global.EnableItemMallBuyFix)
            {
                string uname = session.State["username"] as string;

                if (uname.Length == 0)
                {
                    Global.logmgr.WriteLog(LogLevel.Error, "username len == 0 (request buy mall item)");
                    return PacketProcessResult.Disconnect;

                }

                byte ShopType = pck.ReadUInt8();

                if (ShopType == 24)
                {
                    //------------------------------------------
                    UInt16 uint1 = pck.ReadUInt16();
                    byte b1 = pck.ReadUInt8();
                    byte b2 = pck.ReadUInt8();
                    byte b3 = pck.ReadUInt8();
                    string package_item_codename = pck.ReadAscii();
                    ushort nItems = pck.ReadUInt16();
                    pck.ReadUInt64();
                    uint refpackage_id = pck.ReadUInt32();
                    //------------------------------------------

                    string charname = session.State["charname"] as string;

                    if (charname.Length == 0)
                    {
                        Global.logmgr.WriteLog(LogLevel.Error, "charname len == 0 (buy in item mall)");
                        return PacketProcessResult.Disconnect;
                    }

                    int res = Global.dbmgr.GetBuyMallResult(charname, package_item_codename, nItems);

                    if (res == -1)
                    {
                        //------------------------------------------
                        session.SendClientNotice("UIIT_STT_ANTICHEAT_ITEM_MALL_ERROR");
                        //------------------------------------------
                    }
                    else
                    {
                        //update silk
                        List<int> silk_info = Global.dbmgr.GetSilkDataByUsername(uname);

                        //------------------------------------------
                        Packet resp = new Packet(0x3153);

                        resp.WriteUInt32(silk_info[0]);
                        resp.WriteUInt32(silk_info[1]);
                        resp.WriteUInt32(silk_info[2]);
                        // m_ClientSecurity.Send(resp);
                        session.SendPacketToClient(resp);

                        //------------------------------------------
                        Packet inventory = new Packet(0xB034);
                        inventory.WriteUInt8(1);
                        inventory.WriteUInt8(24);
                        inventory.WriteUInt16(uint1);
                        inventory.WriteUInt8(b1);
                        inventory.WriteUInt8(b2);
                        inventory.WriteUInt8(b3);
                        inventory.WriteUInt8(1);
                        inventory.WriteUInt8(res);
                        inventory.WriteUInt16(nItems);
                        inventory.WriteUInt32(0);
                        //m_ClientSecurity.Send(inventory);
                        session.SendPacketToClient(inventory);
                        //------------------------------------------

                        Packet inventory2 = new Packet(0xB034);
                        inventory2.WriteUInt8(1);
                        inventory2.WriteUInt8(7);
                        inventory2.WriteUInt8((byte)(res));
                        //  m_ClientSecurity.Send(inventory2);
                        session.SendPacketToClient(inventory2);

                        //------------------------------------------

                        //  Global.g_LogManager.WriteLog(LogLevel.Notify, "GetBuyMallResult OK slot {0}", res);
                    }
                    return PacketProcessResult.ContinueLoop;
                }
            }

            return PacketProcessResult.DoNothing;
        }