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

Code before:
                                           nextAddressIndex >= 0 && nextIPv6Addressindex >= 0;


                    if (parallelConnect)
                    {
                        nextAddressIndex = GetNextAddress(addresses, AddressFamily.InterNetwork, -1);
                        nextIPv6Addressindex = GetNextAddress(addresses, AddressFamily.InterNetworkV6, -1);
                        internalArgs.SecondarySaea = new SocketAsyncEventArgs();
                    }

                    internalArgs.SocketError = SocketError.Success;
                    while (true)
                    {
                        if (!parallelConnect)
                        {
                            // We simply try addresses in sequence until we either try them all or operation is cancelled.
                            if (nextAddressIndex >= addresses.Length)
                            {
                                break;
                            }
                            address = addresses[nextAddressIndex];
                            nextAddressIndex++;
                        }
                        else
                        {
                            if (nextAddressIndex == -1 && nextIPv6Addressindex == -1)
                            {
                                // we used all IPv4 & IPv6 addresses
                                break;
                            }

                            if (internalArgs.SocketError != SocketError.IOPending)
                            {
                                if (nextAddressIndex >= 0)
                                {
                                    address = addresses[nextAddressIndex];
                                    nextAddressIndex = GetNextAddress(addresses, currentAddressFamily, nextAddressIndex);

                                }
                                else
                                {
                                    address = null;
                                    currentAddressFamily = AddressFamily.InterNetworkV6;
                                }
                                    internalArgs.SocketError = SocketError.IOPending;
                            }

                            if (internalArgs.SecondarySaea?.SocketError != SocketError.IOPending || nextIPv6Addressindex > 0)
                            {
                                if (nextIPv6Addressindex >= 0)
                                {

Review comment (performance):
```suggestion
                            if (nextAddressIndex < 0 && nextIPv6Addressindex < 0)
```
The JIT can produce less and more optimized machine-code (if it matters here too, but the change is cheap...)

Code after:
                Socket? tempSocketIPv4 = null, tempSocketIPv6 = null;
                Exception? caughtException = null;
                try
                {
                    // Try each address in turn.  We store the last error received, such that if we fail to connect to all addresses,
                    // we can use the last error to represent the entire operation.
                    SocketError lastError = SocketError.NoData;
                    foreach (IPAddress address in await addressesTask.ConfigureAwait(false))
                    {
                        Socket? attemptSocket = null;
                        if (_currentSocket != null)
                        {
                            // If this SocketAsyncEventArgs was configured with a socket, then use it.
                            // If that instance doesn't support this address, move on to the next.
                            if (!_currentSocket.CanTryAddressFamily(address.AddressFamily))
                            {
                                continue;
                            }

                            attemptSocket = _currentSocket;
                        }
                        else
                        {
                            // If this SocketAsyncEventArgs doesn't have a socket, then we need to create a temporary one, which we do
                            // based on this address' address family (and then reuse for subsequent addresses for the same family).
                            if (address.AddressFamily == AddressFamily.InterNetworkV6)
                            {
                                attemptSocket = tempSocketIPv6 ??= (Socket.OSSupportsIPv6 ? new Socket(AddressFamily.InterNetworkV6, socketType, protocolType) : null);
                                if (attemptSocket is not null && address.IsIPv4MappedToIPv6)
                                {
                                    // We need a DualMode socket to connect to an IPv6-mapped IPv4 address.
                                    attemptSocket.DualMode = true;
                                }
                            }
                            else if (address.AddressFamily == AddressFamily.InterNetwork)
                            {
                                attemptSocket = tempSocketIPv4 ??= (Socket.OSSupportsIPv4 ? new Socket(AddressFamily.InterNetwork, socketType, protocolType) : null);
                            }

                            // If we were unable to get a socket to use for this address, move on to the next address.
                            if (attemptSocket is null)
                            {
                                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.
                        attemptSocket.ReplaceHandleIfNecessaryAfterFailedConnect();