public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // Fail when session is disabled or has already started.
        $this->failWhenDisabled();
        $this->failWhenStarted();

        // Try to retrieve the session id from the request cookies.
        $cookies = $request->getCookieParams();

        $session_id = $cookies[$this->name] ?? '';

        if ($session_id != '') session_id($session_id);

        // Handle the request when session_start is successful.
        if (session_start(self::SESSION_OPTIONS)) {

            session_name($this->name);

            $response = $handler->handle($request);

            $this->failWhenClosed();

            $session_id = session_id();

            session_write_close();

            return $this->withSessionCookie($response, $session_id);

        }

        throw new SessionStartException;
    }