public void OnReceive(IByteBuffer buffer)
        {
            if (_udpSocket.State != SocketState.Connecting)
            {
                Log(LogLevel.Error, "Invalid Network Status {0} in connecting phase", _udpSocket.State);
                _udpSocket.Dispose();
                return;
            }

            CommandType type = (CommandType)buffer.ReadByte();

            if (type != CommandType.ConnectResponse)
            {
                Log(LogLevel.Error, "Received a invalid CommandType {0} in connecting phase", type);
                _udpSocket.Dispose();
                return;
            }

            int  peerID          = buffer.ReadInt(); //4
            byte serverKeyLength = buffer.ReadByte();

            byte[] serverKey = new byte[serverKeyLength];
            buffer.ReadBytes(serverKey);
            int  port         = buffer.ReadInt();  //4
            long sendingTime  = buffer.ReadLong(); //8
            long responseTime = buffer.ReadLong(); //8
            long crc          = buffer.ReadLong(); //8

            buffer.SeekWriteIndex(-(int)Lengths.Crc);
            buffer.WriteLong(0);

            long calc = buffer.CalculateCrc();

            if (crc != calc)
            {
                Log(LogLevel.Error, "Invalid crc recv[{0}] calc[{1}]", crc, calc);
                return;
            }

            _peer.Init(peerID, port, new BigInteger(serverKey), sendingTime, responseTime);
        }
        /// <summary>
        /// Called by a receive thread.
        /// </summary>
        void INetworkBroker.OnReceive(IByteBuffer buffer)
        {
            uint currentTime = EnvironmentTimer.GetTickCount();
            int  bufferCount = buffer.Count;

            CommandType ct = (CommandType)buffer.ReadByte();

            if (ct != CommandType.None)
            {
                return;
            }

            //peerID (4 bytes)
            int peerID = buffer.ReadInt();

            if (_peerId > 0 && peerID != _peerId)
            {
                Log(LogLevel.Error, "A assigned peerID [{0}] is different from recevied peerID [{1}] from server", peerID, _peerId);
                Disconnect(DisconnectReason.InvalidConnection, false);
            }

            //timestamp of sent time (8 byte)
            uint serverSentTime = (uint)buffer.ReadLong();

            //the number of commands in a incoming buffer (2 byte)
            short commandCount = buffer.ReadShort();

            //crc value (8 byte)
            long crc = buffer.ReadLong();

            if (IsCrcEnabled)
            {
                int writeIndex = buffer.WriteIndex;

                buffer.WriteIndex = buffer.ReadIndex - (int)Lengths.Crc;
                buffer.WriteLong(0);
                buffer.WriteIndex = writeIndex;

                long calc = buffer.CalculateCrc();

                if (crc != calc)
                {
                    if (_allowStatistics)
                    {
                        _statistics.ErrorCrc();
                    }

                    return;
                }
            }

            for (int i = 0; i < commandCount; i++)
            {
                IncomingCommand command = new IncomingCommand(buffer, currentTime, serverSentTime);

                if (command.IsReliable)
                {
                    SendAckFromCommand(command);
                }

                if (command.Type == CommandType.Acknowledge)
                {
                    AckHandler(command);
                }
                else
                {
                    EnqueueRenderingQueue(() => ExecuteReceiveCommand(command));
                }
            }

            if (_allowStatistics)
            {
                _statistics.ReceiveBytes(bufferCount, currentTime);
                _statistics.ReceiveMtu(serverSentTime);
                _statistics.ReceiveIncomingCommand(commandCount);
            }
        }