/**
  * @Setup
  * @internal
  */
 public function setup()
 {
     $this->loop = new ReactLoopAdapter($this->socketManager);
     $this->socket = new SocketServer($this->loop);
     $this->httpServer = new \React\Http\Server($this->socket);
     $this->wamp = new WampHandler();
     $this->wsServer = new WsServer(new WampServer($this->wamp));
     $that = $this;
     $this->httpServer->on('request', function ($request, $response) use($that) {
         $request = new Request($request);
         $response = new Response($response);
         $session = new Session($that->sessionStorage, $request, $response);
         $httpRequest = new StdClass();
         $httpRequest->request = $request;
         $httpRequest->response = $response;
         $httpRequest->body = '';
         $httpRequest->session = $session;
         $request->on('data', function ($bodyBuffer) use($that, &$httpRequest) {
             $httpRequest->body .= $bodyBuffer;
             if (!$that->isRequestBodyFullyReceived($httpRequest)) {
                 return;
             }
             $that->handleRequest($httpRequest);
             $httpRequest = null;
         });
     });
     // setup handler for root path
     $this->registerHandler("|^/\$|", function ($request, $response) {
         $response->writeHead(200, array('Content-Type' => 'text/plain'));
         $response->end("Hello Budabot!\n");
     });
     // switch server's port if http_server_port setting is changed
     $this->settingManager->registerChangeListener('http_server_port', function ($name, $oldValue, $newValue) use($that) {
         if ($that->isListening) {
             $that->listen($newValue);
         }
     });
     // make sure we close the socket before exit
     register_shutdown_function(function () use($that) {
         $that->stopListening();
     });
 }