private static void AuthChallengeRequestCallback(IRealmClient client)
        {
            if (!client.IsConnected)
            {
                // Client disconnected in the meantime
                return;
            }

            if (BanMgr.IsBanned(client.ClientAddress))
            {
                OnLoginError(client, AccountStatus.CloseClient);
            }
            else
            {
                var acctQuery = new Action(() =>
                {
                    var acc = AccountMgr.GetAccount(client.AccountName);
                    //if(acc != null && DateTime.Now - acc.LastLogin < new TimeSpan(0,0,0,30))
                    //    return;
                    if (acc == null)
                    {
                        if (RealmServerConfiguration.AutocreateAccounts)
                        {
                            QueryAccountCallback(client, null);
                        }
                        else
                        {
                            OnLoginError(client, AccountStatus.WrongLoginOrPass);
                        }
                    }
                    else
                    {
                        if (acc.Password != client.Password)
                        {
                            if (client.ClientAddress != null)
                            {
                                Log.Create(Log.Types.AccountOperations, LogSourceType.Account, (uint)acc.AccountId)
                                .AddAttribute("operation", 1, "login_wrong_pass")
                                .AddAttribute("name", 0, acc.Name)
                                .AddAttribute("ip", 0, client.ClientAddress.ToString())
                                .Write();
                            }
                            OnLoginError(client, AccountStatus.WrongLoginOrPass);
                        }
                        else
                        {
                            QueryAccountCallback(client, acc);
                        }
                    }
                });

                RealmServer.IOQueue.AddMessage(acctQuery);
            }
        }
        private static void QueryAccountCallback(IRealmClient client, Account acct)
        {
            if (client == null || !client.IsConnected)
            {
                return;
            }
            var accName  = client.AccountName;
            var realmAcc = RealmServer.Instance.GetLoggedInAccount(accName);

            if (acct != null && acct.IsLogedOn)
            {
                Log.Create(Log.Types.AccountOperations, LogSourceType.Account, (uint)acct.AccountId)
                .AddAttribute("operation", 1, "account_in_use")
                .AddAttribute("name", 0, acct.Name)
                .AddAttribute("ip", 0, client.ClientAddress.ToString())
                .Write();
                OnLoginError(client, AccountStatus.AccountInUse);
                return;
            }
            if (realmAcc != null && realmAcc.ActiveCharacter != null && realmAcc.Client != null && realmAcc.Client.IsConnected)
            {
                Log.Create(Log.Types.AccountOperations, LogSourceType.Account, (uint)acct.AccountId)
                .AddAttribute("operation", 1, "account_in_use")
                .AddAttribute("name", 0, acct.Name)
                .AddAttribute("ip", 0, client.ClientAddress.ToString())
                .Write();
                OnLoginError(client, AccountStatus.AccountInUse);
                return;
            }


            if (acct == null)
            {
                // Account doesn't exist yet -> Check for auto creation
                if (RealmServerConfiguration.AutocreateAccounts)
                {
                    if (!AccountMgr.NameValidator(ref accName) || client.Password == null || client.Password.Length > 20)
                    {
                        OnLoginError(client, AccountStatus.WrongLoginOrPass);
                        return;
                    }
                    client.AuthAccount = AccountMgr.Instance.CreateAccount(accName, client.Password, "", RealmServerConfiguration.DefaultRole);
                    client.AuthAccount.Save();

                    //client.Account = new RealmAccount(accName,new AccountInfo(){ClientId = ClientId.Original,AccountId = client.AuthAccount.AccountId,EmailAddress = "",LastIP = client.ClientAddress.GetAddressBytes(),LastLogin = DateTime.Now,Locale = ClientLocale.English,RoleGroupName = "Player"});
                    SendAuthChallengeSuccessReply(client);
                }
                else
                {
                    OnLoginError(client, AccountStatus.WrongLoginOrPass);
                    return;
                }
                client.AuthAccount.IsLogedOn = true;
            }
            else
            {
                // check if Account may be used

                if (acct.CheckActive())
                {
                    client.AuthAccount = acct;
                    if (realmAcc == null)
                    {
                        SendAuthChallengeSuccessReply(client);
                    }
                }
                else
                {
                    Log.Create(Log.Types.AccountOperations, LogSourceType.Account, (uint)acct.AccountId)
                    .AddAttribute("operation", 1, "login_banned")
                    .AddAttribute("name", 0, acct.Name)
                    .AddAttribute("ip", 0, client.ClientAddress.ToString())
                    .Write();
                    // Account has been deactivated
                    if (client.AuthAccount == null || client.AuthAccount.StatusUntil == null)
                    {
                        // temporarily suspended
                        OnLoginError(client, AccountStatus.WrongLoginOrPass);
                    }
                    else
                    {
                        // deactivated
                        OnLoginError(client, AccountStatus.WrongLoginOrPass);
                    }
                    return;
                }
            }
            if (realmAcc == null)
            {
                if (acct != null)
                {
                    Log.Create(Log.Types.AccountOperations, LogSourceType.Account, (uint)acct.AccountId)
                    .AddAttribute("operation", 1, "login_ok")
                    .AddAttribute("name", 0, acct.Name)
                    .AddAttribute("ip", 0, client.ClientAddress.ToString())
                    .Write();
                }
                RealmAccount.InitializeAccount(client, client.AuthAccount.Name);
            }
            else if (acct != null)
            {
                if (realmAcc.Client != null)
                {
                    if (realmAcc.Client.ActiveCharacter != null)
                    {
                        realmAcc.Client.ActiveCharacter.SendInfoMsg("Some one loggin in to your account. Disconnecting.");
                    }
                    realmAcc.Client.Disconnect();
                }
                if (client.ClientAddress == null)
                {
                    return;
                }
                realmAcc.LastIP = client.ClientAddress.GetAddressBytes();
                acct.LastIP     = client.ClientAddress.GetAddressBytes();
                acct.Save();
                client.Account = realmAcc;
                if (realmAcc.ActiveCharacter != null)
                {
                    ConnectClientToIngameCharacter(client, acct, realmAcc);
                }
                else
                {
                    Log.Create(Log.Types.AccountOperations, LogSourceType.Account, (uint)acct.AccountId)
                    .AddAttribute("operation", 1, "character_select_menu")
                    .AddAttribute("name", 0, acct.Name)
                    .AddAttribute("ip", 0, client.ClientAddress.ToString())
                    .Write();
                    SendAuthChallengeSuccessReply(client);
                }
            }
        }