protected override void HandleRequest(HttpListenerRequest request, HttpListenerResponse response) { // Paths: // /player/<id>/connect if (request.HttpMethod == "POST") { if (request.Url.Segments.Length > 3) { if (request.Url.Segments[1].Replace("/", "") == "player" && PlayerId.TryParse(request.Url.Segments[2].Replace("/", ""), out PlayerId playerId)) { if (request.Url.Segments[3] == "connect") { var content = request.GetJsonContent <PostPlayerConnectRequestBody>(); var worldType = new WorldType(content.WorldType); var gameWorld = this._gameServer.Commander .RunCommandAsync( new GetWorldByTypeCommand( worldType, createIfNeeded: true)) .Result; var playerConnectionRef = this._gameServer.Commander .RunCommandAsync( new ConnectPlayerServerCommand( gameWorld.InstanceId, playerId, this._encryptionKey)) .Result; if (playerConnectionRef.IsNull) { response.CompleteJsonResponse( 400, new FailureResponseBody { Code = 0, Message = "Server failed to connect the player." }); } else { ref readonly var connection = ref playerConnectionRef.Unref();