private function getFirstIpAddressFromHeader(ServerRequestInterface $serverRequest, string $header): string
    {
        $items       = \explode(',', $serverRequest->getHeaderLine($header));
        $headerValue = \trim(\reset($items));

        if (\ucfirst($header) === 'Forwarded') {
            foreach (\explode(';', $headerValue) as $headerPart) {
                if (\mb_strtolower(\mb_substr($headerPart, 0, 4)) === 'for=') {
                    $for         = \explode(']', $headerPart);
                    $headerValue = \trim(\mb_substr(\reset($for), 4), " \t\n\r\0\x0B" . '"[]');

                    break;
                }
            }
        }

        return $headerValue;
    }