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

Code before:
                        // 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)
                        {
                            lastError = SocketError.OperationAborted;
                            break;
                        }

                        lastError = internalArgs.SocketError;

                        // If multi-connect is no longer possible, terminate propagating the last error.
                        if (!attemptSocket!.CanProceedWithMultiConnect)
                        {
                            break;
                        }

                        internalArgs.Reset();
                    }

                    caughtException = new SocketException((int)lastError);
                }
                catch (ObjectDisposedException)
                {
                    // This can happen if the user closes the socket and is equivalent to a call to CancelConnectAsync.
                    caughtException = new SocketException((int)SocketError.OperationAborted);
                }
                catch (Exception exc)
                {
                    caughtException = exc;
                }
                finally
                {
                    // Close the sockets as needed.
                    if (tempSocketIPv4 != null && !tempSocketIPv4.Connected)
                    {
                        tempSocketIPv4.Dispose();
                    }

Review comment (suggestion):
Can this be null? Wouldn't it be better to handle it like this:
```c#
if (attemptSocket?.CanProceedWithMultiConnect != true)
```

Code after:
                    {
                        _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)
                    {
                        _saea.SetResults(exception!, 0, SocketFlags.None);
                        _saea._currentSocket?.UpdateStatusAfterSocketError(_saea._socketError);
                    }
                }

                if (shouldComplete)
                {
                    // If this is the first final result, we need to complete the operation and release underlying SocketAsyncEventArgs
                    _saea.Complete();
                    if (SocketsTelemetry.Log.IsEnabled()) LogBytesTransferEvents(socket?.SocketType, SocketAsyncOperation.Connect, bytesTransferred);
                    // signal caller we are done.
                    _saea.OnCompleted(_saea);
                }
            }
        }


        internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags)
        {
            SetResults(SocketError.Success, bytesTransferred, flags);

            if (NetEventSource.Log.IsEnabled() && bytesTransferred > 0)
            {
                LogBuffer(bytesTransferred);
            }

            SocketError socketError;
            switch (_completedOperation)
            {
                case SocketAsyncOperation.Accept:
                    // Get the endpoint.
                    SocketAddress remoteSocketAddress = _currentSocket!._rightEndPoint!.Serialize();

                    socketError = FinishOperationAccept(remoteSocketAddress);

                    if (socketError == SocketError.Success)