Repository: dotnet/aspnetcore
File: src/Shared/HttpSys/NativeInterop/SocketAddress.cs (C#)

Code before:
    {
        ArgumentOutOfRangeException.ThrowIfLessThan(size, WriteableOffset);
        Family = family;
        _size = size;
        // Sized to match the native structure
        _buffer = new byte[((size / IntPtr.Size) + 2) * IntPtr.Size]; // sizeof DWORD
    }

    internal AddressFamily Family { get; }

    internal int GetPort()
    {
        return (_buffer[2] << 8 & 0xFF00) | (_buffer[3]);
    }

    internal IPAddress? GetIPAddress()
    {
        if (Family == AddressFamily.InterNetworkV6)
        {
            return GetIpv6Address();
        }
        else if (Family == AddressFamily.InterNetwork)
        {
            return GetIPv4Address();
        }
        else
        {
            return null;
        }
    }

    private IPAddress GetIpv6Address()
    {
        Contract.Assert(_size >= IPv6AddressSize);
        var bytes = new byte[NumberOfIPv6Labels * 2];
        Array.Copy(_buffer, 8, bytes, 0, NumberOfIPv6Labels * 2);
        return new IPAddress(bytes); // TODO: Does scope id matter?
    }

    private IPAddress GetIPv4Address()
    {
        Contract.Assert(_size >= IPv4AddressSize);
        return new IPAddress(new byte[] { _buffer[4], _buffer[5], _buffer[6], _buffer[7] });
    }

    internal static unsafe SocketAddress? CopyOutAddress(IntPtr address)
    {
        var addressFamily = *((ushort*)address);
        if (addressFamily == (ushort)AddressFamily.InterNetwork)
        {
            var v4address = new SocketAddress(AddressFamily.InterNetwork, IPv4AddressSize);

Review comment (suggestion):
Shouldn't this use `NetworkToHostOrder` as well?

Edit: nvm, looks like IPAddress already handles that internally

Code after:
        {
            ADDRESS_FAMILY.AF_INET => new SocketAddressIPv4(*(SOCKADDR_IN*)pSockaddr),
            ADDRESS_FAMILY.AF_INET6 => new SocketAddressIPv6(*(SOCKADDR_IN6*)pSockaddr),
            _ => null
        };
    }

    private sealed class SocketAddressIPv4 : SocketAddress
    {
        private readonly SOCKADDR_IN _sockaddr;

        internal SocketAddressIPv4(in SOCKADDR_IN sockaddr)
        {
            _sockaddr = sockaddr;
        }

        internal override int GetPort()
        {
            // sin_port is network byte order
            return IPAddress.NetworkToHostOrder((short)_sockaddr.sin_port);
        }

        internal override IPAddress? GetIPAddress()
        {
            // address is network byte order
            return new IPAddress(_sockaddr.sin_addr.S_un.S_addr);
        }
    }

    private sealed class SocketAddressIPv6 : SocketAddress
    {
        private readonly SOCKADDR_IN6 _sockaddr;

        internal SocketAddressIPv6(in SOCKADDR_IN6 sockaddr)
        {
            _sockaddr = sockaddr;
        }

        internal override int GetPort()
        {
            // sin6_port is network byte order
            return IPAddress.NetworkToHostOrder((short)_sockaddr.sin6_port);
        }

        internal override IPAddress? GetIPAddress()
        {
            // address is network byte order
            // when CsWin32 gets support for inline arrays, remove 'AsReadOnlySpan' call below.
            // https://github.com/microsoft/CsWin32/issues/1086
            return new IPAddress(_sockaddr.sin6_addr.u.Byte.AsReadOnlySpan()); // TODO: Does scope id matter?
        }