/**
  * Assert that an upgrade to WebSockets is possible.
  * 
  * @param HttpRequest $request
  * @param HttpResponse $response
  * @return HttpResponse when upgrading is impossible, NULL if everything's fine.
  */
 protected function assertUpgradePossible(HttpRequest $request, HttpResponse $response)
 {
     if ($request->getMethod() !== Http::METHOD_GET) {
         return $response->withStatus(Http::CODE_METHOD_NOT_ALLOWED)->withHeader('Allow', 'GET');
     }
     if ($request->getProtocolVersion() !== '1.1') {
         return $response->withStatus(Http::CODE_HTTP_VERSION_NOT_SUPPORTED);
     }
     if (!$request->hasHeader('Sec-WebSocket-Key')) {
         return $response->withStatus(Http::CODE_BAD_REQUEST, 'Missing Sec-Websocket-Key header');
     }
     if ($request->hasHeader('Sec-Websocket-Version') && !in_array('13', $request->getHeader('Sec-Websocket-Version'), true)) {
         return $response->withStatus(Http::CODE_BAD_REQUEST, 'Web socket version 13 required')->withHeader('Sec-WebSocket-Version', '13');
     }
 }