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

Code before:

                        // 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
                            {
                                internalArgs.RemoteEndPoint = new IPEndPoint(address, port);
                            }
                        }

                        // Issue the connect.  If it pends, wait for it to complete.
                        bool? result = attemptSocket?.ConnectAsync(internalArgs);
                        if (result != false)
                        {
                            if (attemptSocket2 != null)
                            {
                                Debug.Assert(internalArgs.SecondarySaea != null);
                                attemptSocket2.ConnectAsync(internalArgs.SecondarySaea);
                            }

                            using (cancellationToken.UnsafeRegister(s => Socket.CancelConnectAsync((SocketAsyncEventArgs)s!), internalArgs))
                            {
                                //var tasks = new List<ValueTask> { new ValueTask(internalArgs, internalArgs.Version) };
                                if (internalArgs.SecondarySaea != null)
                                {
                                    //var tasks = new List<ValueTask> { new ValueTask(internalArgs, internalArgs.Version) };
                                    var t1 = new ValueTask(internalArgs, internalArgs.Version).AsTask();
                                    var t2 = new ValueTask(internalArgs.SecondarySaea, internalArgs.SecondarySaea.Version).AsTask();
                                    await Task.WhenAny(t1, t2).ConfigureAwait(false);
                                }
                                else
                                {
                                    await new ValueTask(internalArgs, internalArgs.Version).ConfigureAwait(false);
                                }
                            }
                        }


Review comment (suggestion):
If the assumption in my previous comment about `address == null` is true (`address == null` meaning that we should terminate), it looks like `attemptSocket` should be never null here.

Code after:
                            // 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);
            public void OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) => _mrvtsc.OnCompleted(continuation, state, token, flags);

            public short Version => _mrvtsc.Version;
            public void Reset() => _mrvtsc.Reset();

            protected override void OnCompleted(SocketAsyncEventArgs e) =>_mrvtsc.SetResult(true);

            public bool ReachedCoordinationPointFirst() => !Interlocked.Exchange(ref _isCompleted, true);
        }

        private sealed class ParallelMultiConnectSocketState
        {
            private bool _isCompleted;
            private int _count;
            private SocketAsyncEventArgs _saea;

            public ParallelMultiConnectSocketState(SocketAsyncEventArgs saea)
            {
                _saea = saea;
            }
            public bool Finished() => Interlocked.Exchange(ref _isCompleted, true);

            public void SetResults(Socket? socket, SocketError socketError, int bytesTransferred, SocketFlags flags, Exception? exception)
            {
                int count = Interlocked.Increment(ref _count);
                bool shouldComplete = false;

                if (socketError == SocketError.Success && exception == null)
                {
                    shouldComplete = !Finished();
                    if (shouldComplete)