public static byte[] ReadBuffered(
            this NetworkStream stream,
            int?maxBytes   = null,
            int bufferSize = 0xFFF)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            using (var ms = new MemoryStream())
            {
                stream.CopyBufferedTo(ms, maxBytes, bufferSize);
                return(ms.ToArray());
            }
        }
        public static object ReadJsonBuffered(this NetworkStream stream, Type type, int payloadSize, Encoding encoding = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            encoding = encoding ?? Encoding.UTF8;

            using (var memoryStream = new MemoryStream())
            {
                stream.CopyBufferedTo(memoryStream, payloadSize);

                return(PapercutIPCommSerializer.FromJson(type, encoding.GetString(memoryStream.ToArray())));
            }
        }