Repository: dotnet/runtime
File: src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs (C#)

Code before:
                throw;
            }

            var hints = new Interop.Winsock.AddressInfoEx { ai_family = family };
            if (!justAddresses)
            {
                hints.ai_flags = AddressInfoHints.AI_CANONNAME;
            }

            SocketError errorCode = (SocketError)Interop.Winsock.GetAddrInfoExW(
                hostName, null, Interop.Winsock.NS_ALL, IntPtr.Zero, &hints, &context->Result, IntPtr.Zero, &context->Overlapped, &GetAddressInfoExCallback, &context->CancelHandle);

            if (errorCode == SocketError.IOPending)
            {
                state.RegisterForCancellation(cancellationToken);
            }
            else if (errorCode == SocketError.TryAgain)
            {
                // WSATRY_AGAIN indicates possible problem with reachability according to docs.
                // However, if servers are really unreachable, we would still get IOPenfing here
                // and final result would be posted via overlapped IO.
                // synchronous failure here may signal issue when GetAddrInfoExW does not work from
                // impersonificated context.
                return Task.FromException(new InvalidOperationException());
            }
            else
            {
                ProcessResult(errorCode, context);
            }

            return state.Task;
        }

        [UnmanagedCallersOnly]
        private static unsafe void GetAddressInfoExCallback(int error, int bytes, NativeOverlapped* overlapped)
        {
            // Can be casted directly to GetAddrInfoExContext* because the overlapped is its first field
            GetAddrInfoExContext* context = (GetAddrInfoExContext*)overlapped;

            ProcessResult((SocketError)error, context);
        }

        private static unsafe void ProcessResult(SocketError errorCode, GetAddrInfoExContext* context)
        {
            try
            {
                GetAddrInfoExState state = GetAddrInfoExState.FromHandleAndFree(context->QueryStateHandle);

                CancellationToken cancellationToken = state.UnregisterAndGetCancellationToken();

                if (errorCode == SocketError.Success)

Review comment (suggestion):
Also, using a dummy exception here is strange.  I think this would be better as a Try method, e.g.
```C#
public static unsafe bool TryGetAddrInfoAsync(string hostName, bool justAddresses, AddressFamily family, CancellationToken cancellationToken, out Task task)
```
or something along those lines.

Code after:
                throw;
            }

            var hints = new Interop.Winsock.AddressInfoEx { ai_family = family };
            if (!justAddresses)
            {
                hints.ai_flags = AddressInfoHints.AI_CANONNAME;
            }

            SocketError errorCode = (SocketError)Interop.Winsock.GetAddrInfoExW(
                hostName, null, Interop.Winsock.NS_ALL, IntPtr.Zero, &hints, &context->Result, IntPtr.Zero, &context->Overlapped, &GetAddressInfoExCallback, &context->CancelHandle);

            if (errorCode == SocketError.IOPending)
            {
                state.RegisterForCancellation(cancellationToken);
            }
            else if (errorCode == SocketError.TryAgain)
            {
                // WSATRY_AGAIN indicates possible problem with reachability according to docs.
                // However, if servers are really unreachable, we would still get IOPending here
                // and final result would be posted via overlapped IO.
                // synchronous failure here may signal issue when GetAddrInfoExW does not work from
                // impersonated context.
                GetAddrInfoExContext.FreeContext(context);
                return null;
            }
            else
            {
                ProcessResult(errorCode, context);
            }

            return state.Task;
        }

        [UnmanagedCallersOnly]
        private static unsafe void GetAddressInfoExCallback(int error, int bytes, NativeOverlapped* overlapped)
        {
            // Can be casted directly to GetAddrInfoExContext* because the overlapped is its first field
            GetAddrInfoExContext* context = (GetAddrInfoExContext*)overlapped;

            ProcessResult((SocketError)error, context);
        }

        private static unsafe void ProcessResult(SocketError errorCode, GetAddrInfoExContext* context)
        {
            try
            {
                GetAddrInfoExState state = GetAddrInfoExState.FromHandleAndFree(context->QueryStateHandle);

                CancellationToken cancellationToken = state.UnregisterAndGetCancellationToken();