private void upgradeToWebSocket() throws IOException, NoSuchAlgorithmException {
		if (!"websocket".equalsIgnoreCase(getFirstHeader("upgrade"))) {
			throw new HttpResponseException(HttpStatusCode.BAD_REQUEST, "BAD REQUEST: Invalid upgrade request");
		}
		int version = getFirstHeaderAsInt("sec-websocket-version");
		if (version < 0) {
			version = getFirstHeaderAsInt("sec-websocket-draft");
		}
		if (version != 13) {
			HttpResponse response = new HttpResponse(HttpStatusCode.BAD_REQUEST, MimeTypes.TEXT, "Unsupported websocket version specification");
			response.addHeader("Sec-WebSocket-Version", "13");
			response.send(this);
			requestClose(false);
			return;
		}
		String key = getFirstHeader("sec-websocket-key");
		if (key == null) {
			throw new HttpResponseException(HttpStatusCode.BAD_REQUEST, "BAD REQUEST: Invalid key");
		}
		WebSocketFactory factory = mFactory.getWebSocketFactory(mUri);
		if (factory == null) {
			throw new HttpResponseException(HttpStatusCode.BAD_REQUEST, "BAD REQUEST: No handler");
		}
		WebSocket ws = factory.createWebSocket(this);
		getSession().setPersonality(ws);
		HttpResponse response = new HttpResponse(HttpStatusCode.SWITCHING_PROTOCOLS);
		response.addHeader("Upgrade", "WebSocket");
		response.addHeader("Connection", "Upgrade");
		MessageDigest md = MessageDigest.getInstance("SHA1");
		md.update((key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes(Text.UTF8_ENCODING));
		response.addHeader("Sec-WebSocket-Accept", Base64.getEncoder().encodeToString(md.digest()));
		response.send(this);
		ws.startConnection();
	}