private function filterPort($port)
    {
        if ($port === null) {
            return $port;
        }

        if (!preg_match('#^[0-9]+$#', (string) $port)) {
            throw new \InvalidArgumentException(sprintf('Invalid port "%s".', $port));
        }

        return (int) $port;
    }