private byte[] ReadMessageWithPacketHeader(byte[] connectionDelimiter, CallbackData callbackData, ref bool socketWasRead)
        {
            byte[] rawBuffer = null;

            BaseSocketConnection connection  = callbackData.Connection;
            MessageBuffer        readMessage = callbackData.Buffer;

            //----- Message with delimiter!
            int delimiterSize = connectionDelimiter.Length + 3;

            bool readPacket = false;
            bool readSocket = false;

            do
            {
                rawBuffer = null;

                if (readMessage.PacketOffSet > delimiterSize)
                {
                    //----- Has Delimiter!
                    for (int i = 0; i < connectionDelimiter.Length; i++)
                    {
                        if (connectionDelimiter[i] != readMessage.PacketBuffer[i])
                        {
                            //----- Bad Delimiter!
                            throw new BadDelimiterException("Message delimiter is different from Host delimiter.");
                        }
                    }

                    //----- Get Length!
                    int messageLength = (readMessage.PacketBuffer[connectionDelimiter.Length] << 16)
                                        + (readMessage.PacketBuffer[connectionDelimiter.Length + 1] << 8)
                                        + (readMessage.PacketBuffer[connectionDelimiter.Length + 2]);

                    if (messageLength > FMessageBufferSize)
                    {
                        throw new MessageLengthException("Message length is greater than Host maximum message length.");
                    }

                    //----- Check Length!
                    if (messageLength == readMessage.PacketOffSet)
                    {
                        //----- Equal -> Get rawBuffer!
                        rawBuffer = readMessage.GetRawBuffer(messageLength, delimiterSize);

                        //----- Decrypt!
                        rawBuffer = CryptUtils.DecryptData(connection, rawBuffer, FMessageBufferSize);

                        readPacket = false;
                        readSocket = false;
                    }
                    else
                    {
                        if (messageLength < readMessage.PacketOffSet)
                        {
                            //----- Less -> Get rawBuffer and fire event!
                            rawBuffer = readMessage.GetRawBuffer(messageLength, delimiterSize);

                            //----- Decrypt!
                            rawBuffer = CryptUtils.DecryptData(connection, rawBuffer, FMessageBufferSize);

                            readPacket = true;
                            readSocket = false;

                            FireOnReceived(connection, rawBuffer, false);
                        }
                        else
                        {
                            if (messageLength > readMessage.PacketOffSet)
                            {
                                //----- Greater -> Read Socket!
                                if (messageLength > readMessage.PacketLength)
                                {
                                    readMessage.Resize(messageLength);
                                }

                                readPacket = false;
                                readSocket = true;
                            }
                        }
                    }
                }
                else
                {
                    if (readMessage.PacketRemaining < delimiterSize)
                    {
                        //----- Adjust room for more!
                        readMessage.Resize(readMessage.PacketLength + delimiterSize);
                    }

                    readPacket = false;
                    readSocket = true;
                }
            } while (readPacket);

            if (readSocket)
            {
                if (connection.Active)
                {
                    //----- Read More!
                    if (connection.Stream != null)
                    {
                        //----- Ssl!
                        connection.Stream.BeginRead(readMessage.PacketBuffer, readMessage.PacketOffSet, readMessage.PacketRemaining, new AsyncCallback(BeginReadCallback), callbackData);
                    }
                    else
                    {
                        //----- Socket!
                        connection.Socket.BeginReceive(readMessage.PacketBuffer, readMessage.PacketOffSet, readMessage.PacketRemaining, SocketFlags.None, new AsyncCallback(BeginReadCallback), callbackData);
                    }
                }
            }

            socketWasRead = readSocket;
            return(rawBuffer);
        }
        private void ReadBytesFromConnection(CallbackData callbackData, int readBytes)
        {
            BaseSocketConnection connection  = callbackData.Connection;
            MessageBuffer        readMessage = callbackData.Buffer;

            //----- Has bytes!
            connection.LastAction = DateTime.Now;

            byte[] rawBuffer     = null;
            bool   socketWasRead = false;

            readMessage.PacketOffSet += readBytes;

            switch (connection.DelimiterType)
            {
            case DelimiterType.dtNone:

                //----- Message with no delimiter!
                rawBuffer = readMessage.GetRawBuffer(readBytes, 0);

                break;

            case DelimiterType.dtPacketHeader:

                //----- Message with packet header!
                rawBuffer = ReadMessageWithPacketHeader(connection.Delimiter, callbackData, ref socketWasRead);

                break;

            case DelimiterType.dtMessageTailExcludeOnReceive:
            case DelimiterType.dtMessageTailIncludeOnReceive:


                //----- Message with tail!
                rawBuffer = ReadMessageWithMessageTail(connection.Delimiter, callbackData, ref socketWasRead);

                break;
            }

            if (rawBuffer != null)
            {
                //----- Decrypt!
                //rawBuffer = CryptUtils.DecryptData(connection, rawBuffer, FMessageBufferSize);

                //----- Fire Event!
                FireOnReceived(connection, rawBuffer, true);
            }

            readMessage  = null;
            callbackData = null;

            if (!socketWasRead)
            {
                //----- Check Queue!
                lock (connection.SyncReadCount)
                {
                    connection.ReadCount--;

                    if (connection.ReadCount > 0)
                    {
                        //----- if it need more receiving, start the receive!
                        MessageBuffer continueReadMessage = new MessageBuffer(FSocketBufferSize);

                        //----- if the read queue has items, start to receive!
                        if (connection.Stream != null)
                        {
                            //----- Ssl!
                            connection.Stream.BeginRead(continueReadMessage.PacketBuffer, continueReadMessage.PacketOffSet, continueReadMessage.PacketRemaining, new AsyncCallback(BeginReadCallback), new CallbackData(connection, continueReadMessage));
                        }
                        else
                        {
                            //----- Socket!
                            connection.Socket.BeginReceive(continueReadMessage.PacketBuffer, continueReadMessage.PacketOffSet, continueReadMessage.PacketRemaining, SocketFlags.None, new AsyncCallback(BeginReadCallback), new CallbackData(connection, continueReadMessage));
                        }
                    }
                }
            }
        }