/// <summary>
        /// The on client disconnected.
        /// </summary>
        /// <param name="clientb">
        /// </param>
        /// <param name="forced">
        /// </param>
        protected override void OnClientDisconnected(ClientBase client, bool forced)
        {
            Client client1 = (Client)client;

            client1.Server.ConnectedClients.Remove(client1.Character.characterId);
            base.OnClientDisconnected(client1, forced);
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="client"></param>
 /// <param name="e"></param>
 public override void Warning(ClientBase client, Exception e)
 {
     base.Warning(client, e);
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="client"></param>
 /// <param name="forced"></param>
 protected override void OnClientDisconnected(ClientBase client, bool forced)
 {
     base.OnClientDisconnected(client, forced);
 }
        /// <summary>
        /// The on client connected.
        /// </summary>
        /// <param name="client">
        /// </param>
        /// <returns>
        /// The on client connected.
        /// </returns>
        protected override bool OnClientConnected(ClientBase client)
        {
            Client client1 = (Client)client;

            byte[] welcomePacket = new byte[]
                {
                    0x00, 0x00, 0x00, 0x22, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
                    // Server Salt (32 Bytes)
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                    , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                };

            byte[] salt = new byte[0x20];
            Random rand = new Random();

            rand.NextBytes(salt);

            client1.ServerSalt = string.Empty;

            for (int i = 0; i < 32; i++)
            {
                // 0x00 Breaks Things
                if (salt[i] == 0)
                {
                    salt[i] = 42; // So we change it to something nicer
                }

                welcomePacket[6 + i] = salt[i];

                client1.ServerSalt += string.Format("{0:x2}", salt[i]);
            }

            client1.Send(welcomePacket);

            return true;
        }
        /// <summary>
        /// Disconnects and removes a client.
        /// <seealso cref="ServerBase.Stop"/>
        /// <seealso cref="ServerBase.RemoveAllClients"/>
        /// </summary>
        /// <param name="client">The client to be disconnected/removed</param>
        /// <param name="forced">Flag indicating if the client was disconnected already</param>
        public void DisconnectClient(ClientBase client, bool forced)
        {
            RemoveClient(client);

            try
            {
                //NOTE: if client.TcpSocket.Connected == false that means the remote host forced the connection
                //to close
                OnClientDisconnected(client, forced);

                client.TcpSocket.Shutdown(SocketShutdown.Both);
                client.TcpSocket.Close();
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception e)
            {
                LogManager.GetLogger(CellDef.CORE_LOG_FNAME).ErrorException("", e);
            }
        }
 /// <summary>
 /// Generates a server debug message.
 /// </summary>
 /// <param name="msg">Text describing the notification.</param>
 /// <param name="parms">The parameters to pass to the function for formatting.</param>
 /// <param name="client">The client that generated the error.</param>
 public virtual void Debug(ClientBase client, string msg, params object[] parms)
 {
     if (log.IsDebugEnabled)
     {
         log.Debug(FormatLogString(client, msg, parms));
     }
 }
        /// <summary>
        /// Generates a server error.
        /// </summary>
        /// <param name="parms">Parameters for formatting the message.</param>
        /// <param name="msg">The message describing the error.</param>
        /// <param name="client">The client that generated the error.</param>
        public void Error(ClientBase client, string msg, params object[] parms)
        {
            if (log.IsErrorEnabled)
            {
                log.Error(FormatLogString(client, msg, parms));
            }

            if (client != null)
            {
                DisconnectClient(client);
            }
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="client"></param>
 /// <returns></returns>
 protected override bool OnClientConnected(ClientBase client)
 {
     return true;
 }
        /// <summary>
        /// Create a string for logging information about a given client given a formatted message and parameters
        /// </summary>
        /// <param name="client">Client which caused the event</param>
        /// <param name="msg">Message describing the event</param>
        /// <param name="parms">Parameters for formatting the message.</param>
        protected static string FormatLogString(ClientBase client, string msg, params object[] parms)
        {
            msg = (parms == null ? msg : string.Format(msg, parms));

            if (client == null)
            {
                return msg;
            }
            else
            {
                return string.Format("({0}) -> {1}", ((IPEndPoint)client.TcpSocket.RemoteEndPoint).ToString(), msg);
            }
        }
        /// <summary>
        /// Generates a server error.
        /// </summary>
        /// <param name="e">An exception describing the error.</param>
        /// <param name="client">The client that generated the error.</param>
        public void Error(ClientBase client, Exception e)
        {
            if (log.IsErrorEnabled)
            {
                log.Error("{0} - {1}", client, e);
                log.ErrorException("", e);
            }

            if (client != null)
            {
                DisconnectClient(client);
            }
        }
        /// <summary>
        /// Called when a client has been disconnected from the server.
        /// </summary>
        /// <param name="client">The client that has been disconnected.</param>
        /// <param name="forced">Indicates if the client disconnection was forced</param>
        protected virtual void OnClientDisconnected(ClientBase client, bool forced)
        {
            client.Cleanup();
            Info(client, Resources.Disconnected);

            ClientDisconnectedHandler handler = ClientDisconnected;

            if (handler != null)
                handler(client, forced);
        }
        /// <summary>
        /// Called when a client has connected to the server.
        /// </summary>
        /// <param name="client">The client that has connected.</param>
        /// <returns>True if the connection is to be accepted.</returns>
        protected virtual bool OnClientConnected(ClientBase client)
        {
            Info(client, Resources.Connected);

            ClientConnectedHandler handler = ClientConnected;

            if (handler != null)
                handler(client);

            return true;
        }
 /// <summary>
 /// Disconnects and removes a client.
 /// <seealso cref="ServerBase.Stop"/>
 /// <seealso cref="ServerBase.RemoveAllClients"/>
 /// </summary>
 /// <param name="client">The client to be disconnected/removed</param>
 public void DisconnectClient(ClientBase client)
 {
     DisconnectClient(client, true);
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="client"></param>
 /// <param name="msg"></param>
 /// <param name="parms"></param>
 public override void Info(ClientBase client, string msg, params object[] parms)
 {
     base.Info(client, msg, parms);
 }
 /// <summary>
 /// Generates a server warning.
 /// </summary>
 /// <param name="e">An exception describing the warning.</param>
 /// <param name="client">The client that generated the error.</param>
 public virtual void Warning(ClientBase client, Exception e)
 {
     if (log.IsWarnEnabled)
     {
         log.Warn("{0} - {1}", client, e);
     }
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="client"></param>
 /// <param name="msg"></param>
 /// <param name="parms"></param>
 public override void Debug(ClientBase client, string msg, params object[] parms)
 {
     base.Debug(client, msg, parms);
 }
 /// <summary>
 /// Generates a server notification.
 /// </summary>
 /// <param name="msg">Text describing the notification.</param>
 /// <param name="parms">The parameters to pass to the function for formatting.</param>
 /// <param name="client">The client that generated the error.</param>
 public virtual void Info(ClientBase client, string msg, params object[] parms)
 {
     if (log.IsWarnEnabled)
     {
         log.Info(FormatLogString(client, msg, parms));
     }
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="client"></param>
        /// <param name="forced"></param>
        protected override void OnClientDisconnected(ClientBase clientBase, bool forced)
        {
            Client client = (Client)clientBase;

            //client.Server.ConnectedClients.Remove(client.CharacterID);
            byte[] m_ID = BitConverter.GetBytes(client.Character.Id);
            Array.Reverse(m_ID);
            Byte[] logoff = new byte[]
                {
                    0xdf, 0xdf, 0x00, 0x0a, 0x00, 0x01, 0x00, 0x1d, 0x00, 0x00, 0x0c, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x36,
                    0x51, 0x00, 0x78, 0x00, 0x00, 0xc3, 0x50, m_ID[0], m_ID[1], m_ID[2], m_ID[3], 0x01
                };
            Announce.PlayfieldOthers(client, logoff);

            client.Character.Purge();

            base.OnClientDisconnected(clientBase, forced);

            //Lua:
            /* Removed Lua Hook
            Program.Script.CallHook("OnClientDisconnect", client);
             */
        }
 /// <summary>
 /// Removes a client from the internal client list.
 /// <seealso cref="ServerBase.RemoveAllClients"/>
 /// </summary>
 /// <param name="client">The client to be removed</param>
 protected void RemoveClient(ClientBase client)
 {
     lock (m_clients)
     {
         if (m_clients.Contains(client))
         {
             m_clients.Remove(client);
         }
     }
 }