private void CallbackReadHeader(IAsyncResult ar)
        {
            ReadMessageToken readMessageToken = ar.AsyncState as ReadMessageToken;

            try
            {
                if (readMessageToken != null)
                {
                    int bytesRead = readMessageToken.Socket.EndReceive(ar);
                    if (bytesRead < readMessageToken.Buffer.Length)
                    {
                        while (bytesRead < readMessageToken.Buffer.Length)
                        {
                            bytesRead += readMessageToken.Socket.Receive(readMessageToken.Buffer, bytesRead,
                                                                         readMessageToken.Buffer.Length - bytesRead, SocketFlags.None);
                        }
                    }
                }
            }
            catch
            {
                if (readMessageToken != null)
                {
                    readMessageToken.Buffer = null;
                }
            }
            finally
            {
                if (readMessageToken != null)
                {
                    readMessageToken.Event.Set();
                }
            }
        }
        public void HandleConnection(object obj)
        {
            if (_socket != null)
            {
                try
                {
                    while (_socket.IsConnected())
                    {
                        // read header first
                        byte[]           headerBytes      = new byte[Message.MSG_HEADER_LENGTH];
                        ReadMessageToken readMessageToken = new ReadMessageToken
                        {
                            Buffer = headerBytes,
                            Event  = new AutoResetEvent(false),
                            Socket = _socket
                        };
                        IAsyncResult result = _socket.BeginReceive(headerBytes, 0, headerBytes.Length, SocketFlags.None,
                                                                   CallbackReadHeader, readMessageToken);

                        result.AsyncWaitHandle.WaitOne();
                        readMessageToken.Event.WaitOne();

                        if (readMessageToken.Buffer != null)
                        {
                            Message.MESSAGE_ID msgType = Message.GetMessageType(readMessageToken.Buffer);
                            int msgLength = Message.GetMessageLength(readMessageToken.Buffer);
                            if (msgLength > 0)
                            {
                                byte[] msgData = new byte[msgLength];
                                ReadCompleteMessage(msgData);
                                HandleMessage(msgType, msgData);
                            }
                            else
                            {
                                HandleMessage(msgType, null);
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    _logger.Log("HandleConnection- " + exception);
                }
                finally
                {
                    _socket.DoClose();
                }
            }
        }