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

Code before:
                                    if (me == null)
                                    {
                                        // primary connection is already done.
                                        socket?.Dispose();
                                    }
                                    else
                                    {
                                        me.OnCompleted(e);
                                    }

                                };
                                if (internalArgs.SecondarySaea.RemoteEndPoint is IPEndPoint existing2)
                                {
                                    existing2.Address = address2;
                                    Debug.Assert(existing2.Port == port);
                                }
                                else
                                {
                                    internalArgs.SecondarySaea.RemoteEndPoint = new IPEndPoint(address2, port);
                                    internalArgs.SecondarySaea.SocketError = SocketError.IOPending;
                                    internalArgs.SecondarySaea.UserToken = internalArgs;
                                }
                            }

                            // If we were unable to get a socket to use for this address, move on to the next address.
                            if (attemptSocket is null && attemptSocket2 == null &&
                                internalArgs.SocketError != SocketError.IOPending && (internalArgs.SecondarySaea == null || internalArgs.SecondarySaea.SocketError != SocketError.IOPending))
                            {
                                continue;
                            }
                        }

                        // Reset the socket if necessary to support another connect.  This is necessary on Unix in particular where
                        // the same socket handle can't be used for another connect, so we swap in a new handle under the covers if
                        // possible.  We do this not just for the 2nd+ address but also for the first in case the Socket was already
                        // used for a connection attempt outside of this call.

                        attemptSocket2?.ReplaceHandleIfNecessaryAfterFailedConnect();

                        // Reconfigure the internal event args for the new address.
                        if (address != null)
                        {
                            Debug.Assert(attemptSocket != null);
                            attemptSocket.ReplaceHandleIfNecessaryAfterFailedConnect();
                            if (internalArgs.RemoteEndPoint is IPEndPoint existing)
                            {
                                existing.Address = address;
                                Debug.Assert(existing.Port == port);
                            }
                            else
                            {

Review comment (suggestion):
NIT: using same type of comparison
```suggestion
                            if (attemptSocket == null && attemptSocket2 == null &&
```

Code after:
                                caughtException is OperationCanceledException ||
                                (caughtException is SocketException se && se.SocketErrorCode == SocketError.OperationAborted))
                            {
                                _currentSocket.Dispose();
                            }
                        }

                        // Store the results.
                        if (caughtException != null)
                        {
                            SetResults(caughtException, 0, SocketFlags.None);
                            _currentSocket?.UpdateStatusAfterSocketError(_socketError);
                        }
                        else
                        {
                            SetResults(SocketError.Success, internalArgs.BytesTransferred, internalArgs.SocketFlags);
                            _connectSocket = _currentSocket = internalArgs.ConnectSocket!;
                        }

                        // Complete the operation.
                        if (SocketsTelemetry.Log.IsEnabled()) LogBytesTransferEvents(_connectSocket?.SocketType, SocketAsyncOperation.Connect, internalArgs.BytesTransferred);

                        Complete();

                        // Clean up after our temporary arguments.
                        internalArgs.Dispose();

                        // If the caller is treating this operation as pending, own the completion.
                        if (!internalArgs.ReachedCoordinationPointFirst())
                        {
                            // Regardless of _flowExecutionContext, context will have been flown through this async method, as that's part
                            // of what async methods do.  As such, we're already on whatever ExecutionContext is the right one to invoke
                            // the completion callback.  This method may have even mutated the ExecutionContext, in which case for telemetry
                            // we need those mutations to be surfaced as part of this callback, so that logging performed here sees those
                            // mutations (e.g. to the current Activity).
                            OnCompleted(this);
                        }
                    }
                }
            }
        }

        private sealed class MultiConnectSocketAsyncEventArgs : SocketAsyncEventArgs, IValueTaskSource
        {
            private ManualResetValueTaskSourceCore<bool> _mrvtsc;
            private bool _isCompleted;

            public MultiConnectSocketAsyncEventArgs() : base(unsafeSuppressExecutionContextFlow: false) { }

            public void GetResult(short token) => _mrvtsc.GetResult(token);
            public ValueTaskSourceStatus GetStatus(short token) => _mrvtsc.GetStatus(token);