Repository: dotnet/runtime
File: src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs (C#)

Code before:
            }

            return pending;
        }

        public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e) =>
                            ConnectAsync(socketType, protocolType,  e, ConnectAlgorithm.Default);
        public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e, ConnectAlgorithm connectAlgorithm)
        {
            ArgumentNullException.ThrowIfNull(e);
            if (connectAlgorithm != ConnectAlgorithm.Default &&
                connectAlgorithm != ConnectAlgorithm.Parallel)
            {
                throw new ArgumentException(SR.Format(SR.net_sockets_invalid_connect_algorithm, connectAlgorithm), nameof(connectAlgorithm));
            }

            if (e.HasMultipleBuffers)
            {
                throw new ArgumentException(SR.net_multibuffernotsupported, nameof(e));
            }
            if (e.RemoteEndPoint == null)
            {
                throw new ArgumentException(SR.Format(SR.InvalidNullArgument, "e.RemoteEndPoint"), nameof(e));
            }

            EndPoint endPointSnapshot = e.RemoteEndPoint;
            DnsEndPoint? dnsEP = endPointSnapshot as DnsEndPoint;

            bool pending;
            if (dnsEP != null)
            {
                Socket? attemptSocket = dnsEP.AddressFamily != AddressFamily.Unspecified ? new Socket(dnsEP.AddressFamily, socketType, protocolType) : null;
                e.StartOperationCommon(attemptSocket, SocketAsyncOperation.Connect);
                e.StartOperationConnect(saeaMultiConnectCancelable: true, userSocket: false);
                try
                {
                    pending = e.DnsConnectAsync(dnsEP, socketType, protocolType, connectAlgorithm);
                }
                catch
                {
                    e.Complete(); // Clear in-use flag on event args object.
                    throw;
                }
            }
            else
            {
                Socket attemptSocket = new Socket(endPointSnapshot.AddressFamily, socketType, protocolType);
                pending = attemptSocket.ConnectAsync(e, userSocket: false, saeaCancelable: true);
            }

            return pending;

Review comment (suggestion):
It is very unlikely that someone would cast an integer to an invalid value, and if it happens we can just go with the `Default` logic. We don't do these kind of strict validations elswhere:

https://github.com/dotnet/runtime/blob/c1d880846ac6c545341a99f880ab0bdfa663c0f1/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs#L573-L575

Code after:
                catch (Exception ex)
                {
                    SocketsTelemetry.Log.AfterConnect(SocketError.NotSocket, e.ConnectActivity, ex.Message);
                    e.ConnectActivity = null;

                    _localEndPoint = null;

                    // Clear in-use flag on event args object.
                    e.Complete();
                    throw;
                }
            }

            return pending;
        }

        public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e) =>
                            ConnectAsync(socketType, protocolType, e, ConnectAlgorithm.Default);
        public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e, ConnectAlgorithm connectAlgorithm)
        {
            ArgumentNullException.ThrowIfNull(e);
            if (connectAlgorithm != ConnectAlgorithm.Default &&
                connectAlgorithm != ConnectAlgorithm.Parallel)
            {
                throw new ArgumentException(SR.Format(SR.net_sockets_invalid_connect_algorithm, connectAlgorithm), nameof(connectAlgorithm));
            }

            if (e.HasMultipleBuffers)
            {
                throw new ArgumentException(SR.net_multibuffernotsupported, nameof(e));
            }
            if (e.RemoteEndPoint == null)
            {
                throw new ArgumentException(SR.Format(SR.InvalidNullArgument, "e.RemoteEndPoint"), nameof(e));
            }

            EndPoint endPointSnapshot = e.RemoteEndPoint;
            DnsEndPoint? dnsEP = endPointSnapshot as DnsEndPoint;

            bool pending;
            if (dnsEP != null)
            {
                Socket? attemptSocket = dnsEP.AddressFamily != AddressFamily.Unspecified ? new Socket(dnsEP.AddressFamily, socketType, protocolType) : null;
                e.StartOperationCommon(attemptSocket, SocketAsyncOperation.Connect);
                e.StartOperationConnect(saeaMultiConnectCancelable: true, userSocket: false);
                try
                {
                    pending = e.DnsConnectAsync(dnsEP, socketType, protocolType, connectAlgorithm, cancellationToken: default);
                }
                catch
                {