private object[] ReadArguments(MsgPackReader reader, string target)
        {
            var subscription = this.Connection.GetSubscription(target);

            object[] args;
            if (subscription == null || subscription.callbacks == null || subscription.callbacks.Count == 0)
            {
                args = reader.ReadValue(typeof(object[])) as object[];
            }
            else
            {
                reader.NextToken();

                if (subscription.callbacks[0].ParamTypes != null)
                {
                    args = new object[subscription.callbacks[0].ParamTypes.Length];
                    for (int i = 0; i < subscription.callbacks[0].ParamTypes.Length; ++i)
                    {
                        args[i] = reader.ReadValue(subscription.callbacks[0].ParamTypes[i]);
                    }
                }
                else
                {
                    args = null;
                }

                reader.NextToken();
            }

            return(args);
        }
        /// <summary>
        /// This function must parse binary representation of the messages into the list of Messages.
        /// </summary>
        public void ParseMessages(BufferSegment segment, ref List <Message> messages)
        {
            messages.Clear();

            int offset = segment.Offset;

            while (offset < segment.Count)
            {
                int length = (int)ReadVarInt(segment.Data, ref offset);

                using (var stream = new System.IO.MemoryStream(segment.Data, offset, length))
                {
                    var buff = BufferPool.Get(MsgPackReader.DEFAULT_BUFFER_SIZE, true);
                    try
                    {
                        var context = new SerializationContext {
                            Options = SerializationOptions.SuppressTypeInformation,
                            ExtensionTypeHandler = CustomMessagePackExtensionTypeHandler.Instance
                        };
                        var reader = new MsgPackReader(stream, context, Endianness.BigEndian, buff);

                        reader.NextToken();
                        reader.NextToken();

                        int messageType = reader.ReadByte();
                        switch ((MessageTypes)messageType)
                        {
                        case MessageTypes.Invocation: messages.Add(ReadInvocation(reader)); break;

                        case MessageTypes.StreamItem: messages.Add(ReadStreamItem(reader)); break;

                        case MessageTypes.Completion: messages.Add(ReadCompletion(reader)); break;

                        case MessageTypes.StreamInvocation: messages.Add(ReadStreamInvocation(reader)); break;

                        case MessageTypes.CancelInvocation: messages.Add(ReadCancelInvocation(reader)); break;

                        case MessageTypes.Ping:

                            // https://github.com/aspnet/AspNetCore/blob/master/src/SignalR/docs/specs/HubProtocol.md#ping-message-encoding-1
                            messages.Add(new Message {
                                type = MessageTypes.Ping
                            });
                            break;

                        case MessageTypes.Close: messages.Add(ReadClose(reader)); break;
                        }

                        reader.NextToken();
                    }
                    finally
                    {
                        BufferPool.Release(buff);
                    }
                }

                offset += length;
            }
        }