public static unsafe void GetAddrInfoAsync(DnsResolveAsyncResult asyncResult)
        {
            GetAddrInfoExContext *context = GetAddrInfoExContext.AllocateContext();

            try
            {
                var state = new GetAddrInfoExState(asyncResult);
                context->QueryStateHandle = state.CreateHandle();
            }
            catch
            {
                GetAddrInfoExContext.FreeContext(context);
                throw;
            }

            AddressInfoEx hints = new AddressInfoEx();

            hints.ai_flags  = AddressInfoHints.AI_CANONNAME;
            hints.ai_family = AddressFamily.Unspecified; // Gets all address families

            SocketError errorCode =
                (SocketError)Interop.Winsock.GetAddrInfoExW(asyncResult.HostName, null, 0 /* NS_ALL*/, IntPtr.Zero, ref hints, out context->Result, IntPtr.Zero, ref context->Overlapped, s_getAddrInfoExCallback, out context->CancelHandle);

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

                if (errorCode != SocketError.Success)
                {
                    state.CompleteAsyncResult(new SocketException((int)errorCode));
                    return;
                }

                AddressInfoEx *result        = context->Result;
                string         canonicalName = null;

                List <IPAddress> addresses = new List <IPAddress>();

                while (result != null)
                {
                    if (canonicalName == null && result->ai_canonname != IntPtr.Zero)
                    {
                        canonicalName = Marshal.PtrToStringUni(result->ai_canonname);
                    }

                    var socketAddress = new ReadOnlySpan <byte>(result->ai_addr, result->ai_addrlen);

                    if (result->ai_family == AddressFamily.InterNetwork)
                    {
                        if (socketAddress.Length == SocketAddressPal.IPv4AddressSize)
                        {
                            addresses.Add(CreateIPv4Address(socketAddress));
                        }
                    }
                    else if (SocketProtocolSupportPal.OSSupportsIPv6 && result->ai_family == AddressFamily.InterNetworkV6)
                    {
                        if (socketAddress.Length == SocketAddressPal.IPv6AddressSize)
                        {
                            addresses.Add(CreateIPv6Address(socketAddress));
                        }
                    }

                    result = result->ai_next;
                }

                if (canonicalName == null)
                {
                    canonicalName = state.HostName;
                }

                state.CompleteAsyncResult(new IPHostEntry
                {
                    HostName    = canonicalName,
                    Aliases     = Array.Empty <string>(),
                    AddressList = addresses.ToArray()
                });
            }
            finally
            {
                GetAddrInfoExContext.FreeContext(context);
            }
        }