protected function injectScript(Response $response)
    {
        if (function_exists('mb_stripos')) {
            $posrFunction   = 'mb_strripos';
            $substrFunction = 'mb_substr';
        } else {
            $posrFunction   = 'strripos';
            $substrFunction = 'substr';
        }

        $content = $response->getContent();
        $pos     = $posrFunction($content, '</body>');

        if (false !== $pos) {
            $script = "livereload.js";

            if ($this->checkServerPresence) {
                // GET is required, as livereload apparently does not support HEAD requests ...
                $request = $this->httpClient->get($script);
                try {
                    $checkResponse = $this->httpClient->send($request);

                    if ($checkResponse->getStatusCode() !== 200) {
                        return;
                    }
                } catch (CurlException $e) {
                    // If error is connection failed, we assume the server is not running
                    if ($e->getCurlHandle()->getErrorNo() === 7) {
                        return;
                    }
                    throw $e;
                }
            }

            $content = $substrFunction($content, 0, $pos) . "\n"
                . '<script src="' . $this->httpClient->getBaseUrl() . $script . '"></script>' . "\n"
                . $substrFunction($content, $pos);

            $response->setContent($content);
        }
    }