public Message Deserialize(Stream stream, out SerializationContext serializationContext)
        {
            serializationContext = null;
            var reader = new StreamReader(stream) { Position = 0 };
            var subTypeInfo = this.packetInspector.FindSubType(reader);

            if (subTypeInfo == null)
            {
                return null;
            }

            var serializer = this.serializerResolver.GetSerializer(subTypeInfo.Type);
            if (serializer == null)
            {
                return null;
            }

            reader.Position = 0;
            serializationContext = new SerializationContext(this.serializerResolver);
            var message = new Message
                              {
                                  Header = (Header)this.headerSerializer.Deserialize(reader, serializationContext), 
                                  Body = (MessageBody)serializer.Deserialize(reader, serializationContext)
                              };
            return message;
        }
        public byte[] Serialize(Message message, out SerializationContext serializationContext)
        {
            Contract.Requires<ArgumentNullException>(message != null);
            Contract.Requires<ArgumentNullException>(message.Header != null);
            Contract.Requires<ArgumentNullException>(message.Body != null);
            Contract.Ensures(Contract.Result<byte[]>() != null);
            Contract.Ensures(Contract.Result<byte[]>().Length >= 16);

            throw new NotImplementedException();
        }
        public Message Deserialize(byte[] packet, out SerializationContext serializationContext)
        {
            Contract.Requires<ArgumentNullException>(packet != null);
            Contract.Requires<ArgumentNullException>(packet.Length >= 16);
            Contract.Ensures(Contract.Result<Message>() != null);
            Contract.Ensures(Contract.Result<Message>().Header != null);
            Contract.Ensures(Contract.Result<Message>().Body != null);

            throw new NotImplementedException();
        }
        public Message Deserialize(byte[] packet, out SerializationContext serializationContext)
        {
            using (var memoryStream = new MemoryStream(packet))
            {
                var message = this.messageSerializer.Deserialize(memoryStream, out serializationContext);
                if (message == null || message.Body == null || message.Header == null)
                {
                    throw new InvalidOperationException();
                }

                return message;
            }
        }
        public byte[] Serialize(Message message, out SerializationContext serializationContext)
        {
            using (var memoryStream = new MemoryStream())
            {
                this.messageSerializer.Serialize(memoryStream, message, out serializationContext);
                var buffer = memoryStream.GetBuffer();
                if (buffer == null || buffer.Length < 16)
                {
                    throw new InvalidOperationException();
                }

                return buffer;
            }
        }
        private object SerializeDeserialize(object obj)
        {
            MemoryStream memoryStream = null;

            var serializerResolver = new DebuggingSerializerResolverBuilder<MessageBody>().Build();
            var serializer = serializerResolver.GetSerializer(obj.GetType());

            try
            {
                memoryStream = new MemoryStream();
                using (var streamWriter = new StreamWriter(memoryStream))
                using (var streamReader = new StreamReader(memoryStream))
                {
                    var serializationContext = new SerializationContext(serializerResolver);
                    serializer.Serialize(streamWriter, serializationContext, obj);
                    var arr = memoryStream.ToArray();
                    Console.WriteLine(BitConverter.ToString(arr));

                    memoryStream.Position = 0;
                    var deserializationContext = new SerializationContext(serializerResolver);
                    var result = serializer.Deserialize(streamReader, deserializationContext);
                    memoryStream = null;
                    return result;
                }
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                }
            }
        }
        public void Serialize(Stream stream, Message message, out SerializationContext serializationContext)
        {
            serializationContext = null;
            var serializer = this.serializerResolver.GetSerializer(message.Body.GetType());
            if (serializer == null)
            {
                return;
            }

            serializationContext = new SerializationContext(this.serializerResolver);
            var writer = new StreamWriter(stream) { Position = 0 };
            this.headerSerializer.Serialize(writer, serializationContext, message.Header);
            serializer.Serialize(writer, serializationContext, message.Body);
            var length = writer.Position;
            writer.Position = 6;
            writer.WriteInt16((short)length);
        }