public function respond()
    {
        // Make sure there always is a response
        if (null === $this->result)
        {
            $this->result = new Result;
            $this->result->setResponse(new Error(500, "No output produced"));
        }
        
        // Close and log all script output that hasn't been cleaned yet
        $this->endAllOutputBuffers($this->target_ob_level);

        // Add Content-Type mime header
        $response = $this->result->getResponse();
        $mime = $response->getMimeTypes();
        if (empty($mime))
        {
            $mime = Request::cli() ? "text/plain" : "text/html";
        }
        elseif (is_array($mime))
        {
            $types = $mime;
            $mime = null;
            foreach ($types as $type)
            {
                if ($this->request->accepts($type))
                {
                    $mime = $type;
                    break;
                }
            }
        }

        // If mime is not accepted, return a 406 response
        if (empty($mime) || !$this->request->accepts($mime))
        {
            $mime = 'text/html';
            $this->result->setResponse(new Error(406, "Not Acceptable", 'No acceptable response can be offered'));
        }

        // Execute hooks
        $hook_params = [
            'responder' => $this,
            'request' => $this->request,
            'mime' => $mime,
        ];

        $hook_params = Hook::execute('Wedeto.HTTP.Responder.Respond', $hook_params);

        // Check if mime type was updated
        if (is_string($hook_params['mime']))
            $mime_charset = $hook_params['mime'];

        $mime_charset = $mime;
        $tp = new FileType('', $mime);
        if ($tp->isPlainText())
            $mime_charset .= '; charset=utf-8';

        // Set mime type
        $this->setHeader('Content-Type', $mime_charset);

        // Add headers from response to the final response
        foreach ($this->result->getAllHeaders() as $key => $value)
            $this->setHeader($key, $value);

        // Store the response code
        $this->setResponseCode($response->getStatusCode());

        // All preparational work is done, time to send stuff to the client
        $this->doOutput($mime);
    }