public void Receive(Func <IReceiveContext, Action <IReceiveContext> > lookupSinkChain, TimeSpan timeout)
        {
            var handler = new TimeoutHandler(timeout);

            handler.Run((timedOut) => _connectionHandler.Use(connection =>
            {
                foreach (var message in connection.Queue.Messages)
                {
                    if (timedOut())
                    {
                        break;
                    }

                    using (var body = new MemoryStream(message.Body))
                    {
                        var context = ReceiveContext.FromBodyStream(body, Address.IsTransactional);

                        context.SetMessageId("{0}".FormatWith(message.Id));
                        context.SetInputAddress(Address);

                        byte[] extension = message.Extension;
                        if (extension.Length > 0)
                        {
                            var headers = TransportMessageHeaders.Create(extension);

                            context.SetContentType(headers["Content-Type"]);
                            context.SetOriginalMessageId(headers["Original-Message-Id"]);
                        }

                        Action <IReceiveContext> receive = lookupSinkChain(context);


                        if (receive != null)
                        {
                            if (!connection.Queue.TryRemoveMessage(message))
                            {
                                continue;
                            }

                            receive(context);
                        }
                    }
                }
            }));
        }
        public void Send(ISendContext context)
        {
            _connectionHandler.Use(connection =>
            {
                var
                message = new SqlMessage {
                    Label = context.MessageType
                };

                if (context.ExpirationTime.HasValue)
                {
                    DateTime value           = context.ExpirationTime.Value;
                    message.TimeToBeReceived = value.Kind == DateTimeKind.Utc ? value - SystemUtil.UtcNow : value - SystemUtil.Now;
                }

                var bodyStream = new MemoryStream();

                context.SerializeTo(bodyStream);

                message.Body = bodyStream.ToArray();

                var headers = new TransportMessageHeaders();

                if (!string.IsNullOrEmpty(context.ContentType))
                {
                    headers.Add("Content-Type", context.ContentType);
                }
                if (!string.IsNullOrEmpty(context.OriginalMessageId))
                {
                    headers.Add("Original-Message-Id", context.OriginalMessageId);
                }

                message.Extension = headers.GetBytes();

                connection.Queue.Enqueue(message);
                Console.WriteLine("{3} Sendt message: Label {1} Size {2}".FormatWith(message.Id, message.Label, message.Body.Length, Address.Uri));
            });
        }