private void ReadLengthCallback(IAsyncResult res)
        {
            try
            {
                var buffObj = (BufferObject)res.AsyncState;
                var readLen = ChannelSocket.EndReceive(res);

                if (readLen >= 4)
                {
                    buffObj.PacketSize = BitConverter.ToInt32(buffObj.RecBuff, 0);
                    if (buffObj.PacketSize < buffObj.BufferSize)
                    {
                        ChannelSocket.BeginReceive(buffObj.RecBuff, 0, buffObj.PacketSize, 0, ReadDataCallback, buffObj);
                    }
                    else
                    {
                        ChannelSocket.BeginReceive(buffObj.RecBuff, 0, buffObj.BufferSize, 0, ReadDataCallback, buffObj);
                    }
                }
            }
            catch
            {
            }
        }
        private void ReadDataCallback(IAsyncResult res)
        {
            var buffObj = (BufferObject)res.AsyncState;

            buffObj.ReadLen    = ChannelSocket.EndReceive(res);
            buffObj.TotalRead += buffObj.ReadLen;

            buffObj.CompleteBuff.AddRange(FastBuffer.SliceBuffer(buffObj.RecBuff, 0, buffObj.ReadLen));
            if (buffObj.CompleteBuff.Count < buffObj.PacketSize)
            {
                // keep reading
                if (buffObj.BufferSize < (buffObj.PacketSize - buffObj.CompleteBuff.Count))
                {
                    ChannelSocket.BeginReceive(buffObj.RecBuff, 0, buffObj.BufferSize, 0, ReadDataCallback, buffObj);
                }
                else
                {
                    ChannelSocket.BeginReceive(buffObj.RecBuff, 0, (buffObj.PacketSize - buffObj.CompleteBuff.Count),
                                               0, ReadDataCallback, buffObj);
                }
            }
            else
            {
                // full message was received
                var dataBuff    = buffObj.CompleteBuff.ToArray();
                var isOperation = BitConverter.ToBoolean(dataBuff, 0);
                int internalId;
                var operationGuid  = Guid.Empty;
                var connectionGuid = Guid.Empty;
                if (isOperation)
                {
                    operationGuid = new Guid(dataBuff.Slice(2, 16));
                    Debug.Print("Received operation guid: " + operationGuid);
                    connectionGuid = new Guid(dataBuff.Slice(18, 16));
                    internalId     = BitConverter.ToInt32(dataBuff, 34);
                }
                else
                {
                    internalId = BitConverter.ToInt32(dataBuff, 2);
                }

                if (_internalPacketTbl.ContainsKey(internalId))
                {
                    var packet = (IDataPacket)Activator.CreateInstance(_internalPacketTbl[internalId]);
                    var ms     = new MemoryStream(dataBuff)
                    {
                        Position = 2
                    };
                    packet.DeserializeFrom(ms);

                    if (packet is SynchronizePacket)
                    {
                        HandleSynchronizePacket(packet as SynchronizePacket);
                    }
                    else if (packet is SequenceInitPacket)
                    {
                        HandleSequenceInitPacket(packet as SequenceInitPacket);
                    }
                    else if (packet is SequencePacket)
                    {
                        HandleSequencePacket(packet as SequencePacket);
                    }
                    else if (packet is PeerRelocationRequestPacket)
                    {
                        HandleRelocationPacket(packet as PeerRelocationRequestPacket);
                    }
                    else if (packet is SocketOperationRequest)
                    {
                        HandleSocketOperationPacket(packet as SocketOperationRequest);
                    }
                }
                else if (ActiveSocketOperations.ContainsKey(operationGuid))
                {
                    using (var ms = new MemoryStream(dataBuff)
                    {
                        Position = 34
                    })
                    {
                        var packet = PacketProcessor.ParsePacket(ms);
                        packet.DeserializeFrom(ms);
                        ActiveSocketOperations[operationGuid].PacketReceived(packet, this);
                    }
                }
                else
                {
                    RawPacketReceived(dataBuff);
                }
                buffObj.ReadLen = 0;
                buffObj.CompleteBuff.Clear();
                buffObj.PacketSize = 0;
                ChannelSocket.BeginReceive(buffObj.RecBuff, 0, 4, 0, ReadLengthCallback, buffObj);
            }
        }