protected function handleRequest(ServerRequestInterface $request)
    {
        if ($this->getStaticDirectory()) {
            $staticResponse = $this->serveStatic($request);
            if ($staticResponse instanceof ResponseInterface) {
                return $staticResponse;
            }
        }

        if ($bridge = $this->getBridge()) {
            try {
                $response = $bridge->handle($request);
            } catch (\Throwable $t) {
                error_log(
                    'An exception was thrown by the bridge. Forcing restart of the worker. The exception was: ' .
                    (string)$t
                );
                $response = new Response(500, [], 'Unexpected error');

                @ob_end_clean();
                $this->shutdown();
            }
            $this->sendCurrentFiles();
        } else {
            $response = new Response(404, [], 'No Bridge defined');
        }

        if (headers_sent()) {
            //when a script sent headers the cgi process needs to die because the second request
            //trying to send headers again will fail (headers already sent fatal). Its best to not even
            //try to send headers because this break the whole approach of php-pm using php-cgi.
            error_log(
                'Headers have been sent, but not redirected to client. Forcing restart of the worker. ' .
                'Make sure your application does not send headers on its own.'
            );
            $this->shutdown();
        }
        $this->sendMessage($this->controller, 'stats', ['memory_usage' => round(memory_get_peak_usage(true)/1048576, 2)]); // Convert memory usage to MB
        return $response;
    }