System.Net.Sockets.SafeCloseSocket.InnerSafeCloseSocket.CreateSocket C# (CSharp) Method

CreateSocket() public static method

public static CreateSocket ( AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, SocketError &errorCode ) : InnerSafeCloseSocket
addressFamily AddressFamily
socketType SocketType
protocolType ProtocolType
errorCode SocketError
return InnerSafeCloseSocket
            public static unsafe InnerSafeCloseSocket CreateSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, out SocketError errorCode)
            {
                IntPtr fd;
                Interop.Error error = Interop.Sys.Socket(addressFamily, socketType, protocolType, &fd);
                if (error == Interop.Error.SUCCESS)
                {
                    Debug.Assert(fd != (IntPtr)(-1), "fd should not be -1");

                    errorCode = SocketError.Success;

                    // The socket was created successfully; enable IPV6_V6ONLY by default for AF_INET6 sockets.
                    if (addressFamily == AddressFamily.InterNetworkV6)
                    {
                        int on = 1;
                        error = Interop.Sys.SetSockOpt(fd, SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, (byte*)&on, sizeof(int));
                        if (error != Interop.Error.SUCCESS)
                        {
                            Interop.Sys.Close(fd);
                            fd = (IntPtr)(-1);
                            errorCode = SocketPal.GetSocketErrorForErrorCode(error);
                        }
                    }
                }
                else
                {
                    Debug.Assert(fd == (IntPtr)(-1), $"Unexpected fd: {fd}");

                    errorCode = SocketPal.GetSocketErrorForErrorCode(error);
                }

                var res = new InnerSafeCloseSocket();
                res.SetHandle(fd);
                return res;
            }

Same methods

SafeCloseSocket.InnerSafeCloseSocket::CreateSocket ( IntPtr fileDescriptor ) : InnerSafeCloseSocket