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

Code before:
                                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))
                            {
                                if (internalArgs.SecondarySaea != null)
                                {
                                    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);
                                }
                            }
                        }

                        // If it completed successfully, we're done; cleanup will be handled by the finally.
                        if (internalArgs.SocketError == SocketError.Success)
                        {
                            if (internalArgs.SecondarySaea != null)
                            {
                                internalArgs.SecondarySaea.UserToken = null;
                            }
                            return;
                        }

                        if (internalArgs.SecondarySaea != null && internalArgs.SecondarySaea.SocketError == SocketError.Success)
                        {
                            return;
                        }

                        // If the operation was canceled, simulate the appropriate SocketError.
                        if (cancellationToken.IsCancellationRequested)

Review comment (bug):
Wouldn't this potentially lead to unobserved exceptions? 
Also if one of the tasks fails, is it expected here that it will throw here?

Code after:

            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)
                    {
                        _saea._connectSocket = _saea._currentSocket = socket;
                        _saea.SetResults(SocketError.Success, bytesTransferred, flags);
                    }
                    else
                    {
                        // Another parallel connect already won - dispose the losing socket.
                        socket?.Dispose();
                    }
                }
                else if (count == 2)    // We ignore failures on first socket since we have one more pending.
                {
                    shouldComplete = !Finished();
                    if (shouldComplete)
                    {