public static unsafe InnerSafeCloseSocket CreateSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) { int af = SocketPal.GetPlatformAddressFamily(addressFamily); int sock = SocketPal.GetPlatformSocketType(socketType); int pt = (int)protocolType; int fd = Interop.libc.socket(af, sock, pt); if (fd != -1) { // The socket was created successfully; make it non-blocking and enable // IPV6_V6ONLY by default for AF_INET6 sockets. int err = Interop.Sys.Fcntl.SetIsNonBlocking(fd, 1); if (err != 0) { Interop.Sys.Close(fd); fd = -1; } else if (addressFamily == AddressFamily.InterNetworkV6) { int on = 1; err = Interop.libc.setsockopt(fd, Interop.libc.IPPROTO_IPV6, Interop.libc.IPV6_V6ONLY, &on, (uint)sizeof(int)); if (err != 0) { Interop.Sys.Close(fd); fd = -1; } } } var res = new InnerSafeCloseSocket(); res.SetHandle((IntPtr)fd); return(res); }