Setting socket's IP protection level causes an error only in unity

I’m making a multiplayer game, and I’m currently trying to get automatic portmapping to work. When making a socket and setting its IP protection in visual studio, there’s no errors, but in unity, it says:

“SocketException: An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call.”

The level of protection I’m trying to use is unrestricted, but I’ve tested the other levels, and they don’t work either.

I am using Open.NAT for the portmapping, and I am using Unity 2021.3.9f1.

Here is the code:

public class AutomaticPortMapping : MonoBehaviour
{
    public async void Start()
    {
        NatDiscoverer discoverer = new NatDiscoverer();

        // using SSDP protocol, it discovers NAT device.
        NatDevice device = await discoverer.DiscoverDeviceAsync();

        // display the NAT's IP address
        print("The external IP Address is: " + await device.GetExternalIPAsync());

        // create a new mapping in the router [external_ip:1702 -> host_machine:1602]
        await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 1602, 1702, "For testing"));

        // configure a TCP socket listening on port 1602
        IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 1602);
        Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        socket.SetIPProtectionLevel(IPProtectionLevel.Unrestricted);
        socket.Bind(endPoint);
        socket.Listen(4);
    }
}

The code is directly copied from Open.NAT - A NAT Traversal library for .NET and Mono - CodeProject, so it should work, but it might be outdated.

I’m not sure why the IP protection level wouldn’t be able to be set in Unity, but have you tried simply not setting it? My understanding is that it might have been required on Windows XP where the default is edge-restricted, but I believe the default protection level has been relaxed starting in Vista.

I can’t use my computer right now, but I have tried not setting it before. If I remember what happens properly, I think no errors happen, but when seeing if the port is open on a website like https://canyouseeme.org, the website fails to see it.