public function getClient($autoLogin = true, $reset = false)
    {
        if (!isset(self::$client) || $reset) {
            $connectorOptions = [];
            $connectorOptions['accounts'] = rtrim($this->config->get('api.accounts_api_url'), '/') . '/';
            $connectorOptions['verify'] = !$this->config->get('api.skip_ssl');
            $connectorOptions['debug'] = $this->config->get('api.debug') ? STDERR : false;
            $connectorOptions['client_id'] = $this->config->get('api.oauth2_client_id');
            $connectorOptions['user_agent'] = $this->getUserAgent();
            $connectorOptions['api_token'] = $this->apiToken;
            $connectorOptions['api_token_type'] = $this->apiTokenType;

            // Proxy support with the http_proxy or https_proxy environment
            // variables.
            if (PHP_SAPI === 'cli') {
                $proxies = [];
                foreach (['https', 'http'] as $scheme) {
                    $proxies[$scheme] = str_replace('http://', 'tcp://', getenv($scheme . '_proxy'));
                }
                $proxies = array_filter($proxies);
                if (count($proxies)) {
                    $connectorOptions['proxy'] = count($proxies) == 1 ? reset($proxies) : $proxies;
                }
            }

            $connector = new Connector($connectorOptions);

            // Set up a persistent session to store OAuth2 tokens. By default,
            // this will be stored in a JSON file:
            // $HOME/.platformsh/.session/sess-cli-default/sess-cli-default.json
            $session = $connector->getSession();
            $session->setId('cli-' . $this->sessionId);

            $this->sessionStorage = KeychainStorage::isSupported()
                && $this->config->isExperimentEnabled('use_keychain')
                ? new KeychainStorage($this->config->get('application.name'))
                : new File($this->config->getSessionDir());
            $session->setStorage($this->sessionStorage);

            // Ensure session data is (re-)loaded every time.
            // @todo move this to the Session
            if (!$session->getData()) {
                $session->load(true);
            }

            self::$client = new PlatformClient($connector);

            if ($autoLogin && !$connector->isLoggedIn()) {
                $this->dispatcher->dispatch('login_required');
            }

            try {
                $connector->getClient()->getEmitter()->on('error', function (ErrorEvent $event) {
                    if ($event->getResponse() && $event->getResponse()->getStatusCode() === 403) {
                        $this->on403($event);
                    }
                });
            } catch (\RuntimeException $e) {
                // Ignore errors if the user is not logged in at this stage.
            }
        }

        return self::$client;
    }