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

Code before:
                                // 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)
                                {
                                    address2 = addresses[nextIPv6AddressIndex];
                                    if (address == null)
                                    {
                                        // fall-back to normal processing without extra SAE
                                        address = addresses[nextIPv6AddressIndex];
                                        internalArgs.SocketError = SocketError.IOPending;
                                        address2 = null;
                                        internalArgs.SecondarySaea = null;
                                    }
                                    else
                                    {
                                        Debug.Assert(internalArgs.SecondarySaea != null);
                                        address2 = addresses[nextIPv6AddressIndex];
                                        internalArgs.SecondarySaea!.SocketError = SocketError.IOPending;
                                    }

                                    nextIPv6AddressIndex = GetNextAddress(addresses, AddressFamily.InterNetworkV6, nextIPv6AddressIndex);
                                }
                                else
                                {
                                    address2 = null;
                                }
                            }
                        }

                        Socket? attemptSocket = null;
                        Socket? attemptSocket2 = null;

Review comment (suggestion):
```suggestion
                                    if (address is not null)
```

Code after:
                                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();

                        // Reconfigure the internal event args for the new address.
                        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.
                        if (attemptSocket.ConnectAsync(internalArgs, userSocket: true, saeaMultiConnectCancelable: false, cancellationToken))
                        {
                            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)
                        {
                            return;
                        }

                        // If the operation was canceled, simulate the appropriate SocketError.
                        if (cancellationToken.IsCancellationRequested)
                        {
                            lastError = SocketError.OperationAborted;