private static void ProcessValue(RedisPipelineItem redisItem, byte[] value)
        {
            if (value == null)
            {
                redisItem.OnSuccess(null);
                return;
            }

            var result = new byte[1][];

            result[0] = value;
            redisItem.OnSuccess(result);
        }
        public async Task ReadAsync(RedisPipelineItem redisItem)
        {
            // Everything is inline to prevent unncessary task allocations
            // this code is called A LOT
            await ReadResponseAsync().ConfigureAwait(false);

            if (CurrentPosition >= CurrentResponse.Count)
            {
                await ReadNextResponseAsync().ConfigureAwait(false);
            }

            var firstChar = ReadFirstChar();

            if (firstChar == RedisProtocolContants.SimpleString)
            {
                if (CurrentPosition >= CurrentResponse.Count)
                {
                    await ReadNextResponseAsync().ConfigureAwait(false);
                }

                var bytes       = new List <byte>();
                var response    = (IList <byte>)CurrentResponse;
                var currentChar = response[CurrentPosition];
                while (currentChar != RedisProtocolContants.LineFeed)
                {
                    if (currentChar != RedisProtocolContants.CarriageReturn)
                    {
                        bytes.Add(currentChar);
                    }

                    CurrentPosition++;
                    if (CurrentPosition >= CurrentResponse.Count)
                    {
                        await ReadNextResponseAsync().ConfigureAwait(false);

                        response = CurrentResponse;
                    }

                    currentChar = response[CurrentPosition];
                }

                CurrentPosition++;
                ProcessValue(redisItem, bytes.ToArray());
            }
            else if (firstChar == RedisProtocolContants.Integer)
            {
                if (CurrentPosition >= CurrentResponse.Count)
                {
                    await ReadNextResponseAsync().ConfigureAwait(false);
                }

                var bytes       = new List <byte>();
                var response    = (IList <byte>)CurrentResponse;
                var currentChar = response[CurrentPosition];
                while (currentChar != RedisProtocolContants.LineFeed)
                {
                    if (currentChar != RedisProtocolContants.CarriageReturn)
                    {
                        bytes.Add(currentChar);
                    }

                    CurrentPosition++;
                    if (CurrentPosition >= CurrentResponse.Count)
                    {
                        await ReadNextResponseAsync().ConfigureAwait(false);

                        response = CurrentResponse;
                    }

                    currentChar = response[CurrentPosition];
                }

                CurrentPosition++;
                ProcessValue(redisItem, bytes.ToArray());
            }
            else if (firstChar == RedisProtocolContants.BulkString)
            {
                var length = await ReadLengthAsync().ConfigureAwait(false);

                if (length == -1)
                {
                    ProcessValue(redisItem, null);
                    return;
                }

                var bytes    = new List <byte>();
                var response = (IList <byte>)CurrentResponse;
                for (var i = 0; i < length; i++)
                {
                    if (CurrentPosition >= CurrentResponse.Count)
                    {
                        await ReadNextResponseAsync().ConfigureAwait(false);

                        response = CurrentResponse;
                    }

                    var currentChar = response[CurrentPosition];
                    bytes.Add(currentChar);
                    CurrentPosition++;
                }

                for (var i = 0; i < 2; i++)
                {
                    if (CurrentPosition >= CurrentResponse.Count)
                    {
                        await ReadNextResponseAsync().ConfigureAwait(false);
                    }

                    CurrentPosition++;
                }

                ProcessValue(redisItem, bytes?.ToArray());
            }
            else if (firstChar == RedisProtocolContants.Array)
            {
                var value = await ReadArrayAsync().ConfigureAwait(false);

                redisItem.OnSuccess(value);
            }
            else if (firstChar == RedisProtocolContants.Error)
            {
                if (CurrentPosition >= CurrentResponse.Count)
                {
                    await ReadNextResponseAsync().ConfigureAwait(false);
                }

                var bytes       = new List <byte>();
                var response    = (IList <byte>)CurrentResponse;
                var currentChar = response[CurrentPosition];
                while (currentChar != RedisProtocolContants.LineFeed)
                {
                    if (currentChar != RedisProtocolContants.CarriageReturn)
                    {
                        bytes.Add(currentChar);
                    }

                    CurrentPosition++;
                    if (CurrentPosition >= CurrentResponse.Count)
                    {
                        await ReadNextResponseAsync().ConfigureAwait(false);

                        response = CurrentResponse;
                    }

                    currentChar = response[CurrentPosition];
                }

                CurrentPosition++;
                var errorText = Encoding.UTF8.GetString(bytes.ToArray());
                var exception = new RedisException(errorText);
                redisItem.OnError(exception);
            }
            else
            {
                var exception = new RedisException("Could not process Redis response.");
                throw exception; // restart the pipeline.
            }
        }