public GossipConnection(TcpClient client)
        {
            if (client == null) throw new ArgumentNullException("client");
            Client = client;
            RemoteEndPoint = client.Client.RemoteEndPoint;
            TimestampConnected = GossipTimestampProvider.CurrentTimestamp;
            TimestampLastActivity = TimestampConnected;

            Stream = new BinaryNetworkStream(client.GetStream());
            Stream.OnDisconnected += s => OnDisconnected(this);
        }
        public async Task WriteToStreamAsync(BinaryNetworkStream stream)
        {
            var intBuf = BitConverter.GetBytes(MessageType);
            await stream.WriteAsync(intBuf, 0, intBuf.Length);

            var buf = Serialize();

            intBuf = BitConverter.GetBytes(buf.Length);
            await stream.WriteAsync(intBuf, 0, intBuf.Length);

            await stream.WriteAsync(buf, 0, buf.Length);
        }
        public GossipConnection(TcpClient client)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }
            Client                = client;
            RemoteEndPoint        = client.Client.RemoteEndPoint;
            TimestampConnected    = GossipTimestampProvider.CurrentTimestamp;
            TimestampLastActivity = TimestampConnected;

            Stream = new BinaryNetworkStream(client.GetStream());
            Stream.OnDisconnected += s => OnDisconnected(this);
        }
        public static async Task <GossipMessage> ReadFromStreamAsync(BinaryNetworkStream stream)
        {
            var intBuf = new byte[4];

            var read = await stream.ReadAsync(intBuf, 0, intBuf.Length);

            if (read != 4)
            {
                return(null);
            }
            int messageType = BitConverter.ToInt32(intBuf, 0);

            read = await stream.ReadAsync(intBuf, 0, intBuf.Length);

            if (read != 4)
            {
                return(null);
            }
            int size = BitConverter.ToInt32(intBuf, 0);

            if (size < 0)
            {
                return(null);
            }
            if (size == 0)
            {
                return(GossipMessageFactory.CreateMessage(messageType, _blankArray));
            }

            var result = new byte[size];
            int offset = 0;

            while (true)
            {
                read = await stream.ReadAsync(result, offset, size - offset);

                if (read == 0)
                {
                    return(null);
                }
                offset += read;
                if (offset >= size)
                {
                    return(GossipMessageFactory.CreateMessage(messageType, result));
                }
            }
        }