private void WriteRequests(int limit)
        {
            void WriteBuffer(ArraySegment <byte> buffer)
            {
                _physicalConnection.Write(buffer.Array, buffer.Offset, buffer.Count);
            }

            Tuple <ArraySegment <byte>, ArraySegment <byte> > GetRequest()
            {
                lock (_lock)
                {
                    if (_buffer.Count > 0)
                    {
                        return(_buffer.Dequeue());
                    }
                }

                return(null);
            }

            Tuple <ArraySegment <byte>, ArraySegment <byte> > request;
            var count = 0;

            while ((request = GetRequest()) != null)
            {
                _clientOptions?.LogWriter?.WriteLine($"Writing request: headers {request.Item1.Count} bytes, body {request.Item2.Count} bytes.");

                WriteBuffer(request.Item1);
                WriteBuffer(request.Item2);

                _clientOptions?.LogWriter?.WriteLine($"Wrote request: headers {request.Item1.Count} bytes, body {request.Item2.Count} bytes.");

                count++;
                if (limit > 0 && count > limit)
                {
                    break;
                }
            }

            lock (_lock)
            {
                if (_buffer.Count == 0)
                {
                    _newRequestsAvailable.Reset();
                }
            }

            _physicalConnection.Flush();
        }
        private void WriteRequests(int bufferLength, int limit)
        {
            void WriteBuffer(ArraySegment <byte> buffer)
            {
                _physicalConnection.Write(buffer.Array, buffer.Offset, buffer.Count);
            }

            bool GetRequest(out ArraySegment <byte> result)
            {
                lock (_lock)
                {
                    if (_buffer.Count > 0)
                    {
                        _remaining = _buffer.Count + 1;
                        result     = _buffer.Dequeue();
                        return(true);
                    }
                }

                result = default(ArraySegment <byte>);
                return(false);
            }

            ArraySegment <byte> request;
            var    count  = 0;
            UInt64 length = 0;
            var    list   = new List <ArraySegment <byte> >();

            while (GetRequest(out request))
            {
                _clientOptions?.LogWriter?.WriteLine($"Writing request: {request.Count} bytes.");
                length += (uint)request.Count;

                list.Add(request);
                _clientOptions?.LogWriter?.WriteLine($"Wrote request: {request.Count} bytes.");

                count++;
                if ((limit > 0 && count > limit) || length > (ulong)bufferLength)
                {
                    break;
                }
            }

            if (list.Count > 0)
            {
                // merge requests into one buffer
                var result   = new byte[length];
                int position = 0;
                foreach (var r in list)
                {
                    Buffer.BlockCopy(r.Array, r.Offset, result, position, r.Count);
                    position += r.Count;
                }

                WriteBuffer(new ArraySegment <byte>(result));
            }

            lock (_lock)
            {
                if (_buffer.Count == 0)
                {
                    _newRequestsAvailable.Reset();
                }
            }

            _physicalConnection.Flush();
        }