public IHandler Negotiate(IRequest request, Func<byte[], Task> sendAction)
        {
            var path = new PathBuilder(request.Path).Build();

            var route = _routeResolver.Resolve(path);
            if (route.Equals(Route.Empty))
                throw new HandshakeException(HttpStatusCode.NotFound);

            var webSocketHandler = route.Handler();

            var version = request.GetWebSocketVersion();

            var handler = _protocolSelector.Select(version, m => webSocketHandler.OnMessage(m), () => webSocketHandler.OnClose(), b => webSocketHandler.OnBinary(b), b => webSocketHandler.OnPing(b), b => webSocketHandler.OnPong(b), ex => webSocketHandler.OnError(ex));

            var routeParameters = _routeParametersBuilder.Build(route.Path, path);

            var queryParameters = _queryParametersBuilder.Build(path);

            var subProtocol = _subProtocolNegotiator.Negotiate(_hostConfiguration.SupportedSubProtocols, request.GetWebSocketProtocols());

            webSocketHandler.Query = queryParameters;
            webSocketHandler.Route = routeParameters;
            webSocketHandler.Context = request.ToContext(subProtocol);
            webSocketHandler.SendAction = (message, frameType) =>
            {
                switch (frameType)
                {
                    case FrameType.Binary:
                        sendAction(handler.FrameBinary(message));
                        break;
                    case FrameType.Ping:
                        sendAction(handler.FramePing(message));
                        break;
                    case FrameType.Pong:
                        sendAction(handler.FramePong(message));
                        break;
                    case FrameType.Text:
                        sendAction(handler.FrameText(message));
                        break;
                    case FrameType.Close:
                        sendAction(handler.FrameClose(WebSocketStatusCode.NormalClosure));
                        break;
                }
            };

            var requestKey = request.Headers["Sec-WebSocket-Key"];

            var response = new WebSocketAcceptResponse(requestKey, subProtocol);
            sendAction(response.ToBytes()).ContinueWith(t => webSocketHandler.OnOpen());

            return handler;
        }