private void TcpBeginConnect <TMessage>(DnsClientAsyncState <TMessage> state)
            where TMessage : DnsMessageBase, new()
        {
            if (state.EndpointInfoIndex == state.EndpointInfos.Count)
            {
                state.TcpStream = null;
                state.TcpClient = null;
                state.SetCompleted();
                return;
            }

            TcpBeginConnect(state, state.EndpointInfos[state.EndpointInfoIndex].ServerAddress);
        }
        private void UdpBeginSend <TMessage>(DnsClientAsyncState <TMessage> state)
            where TMessage : DnsMessageBase, new()
        {
            if (state.EndpointInfoIndex == state.EndpointInfos.Count)
            {
                state.UdpClient   = null;
                state.UdpEndpoint = null;
                state.SetCompleted();
                return;
            }

            try
            {
                DnsClientEndpointInfo endpointInfo = state.EndpointInfos[state.EndpointInfoIndex];

                state.UdpEndpoint = new IPEndPoint(endpointInfo.ServerAddress, _port);

                state.UdpClient = new System.Net.Sockets.Socket(state.UdpEndpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);

                PrepareAndBindUdpSocket(endpointInfo, state.UdpClient);

                state.TimedOut      = false;
                state.TimeRemaining = QueryTimeout;

                IAsyncResult asyncResult = state.UdpClient.BeginSendTo(state.QueryData, 0, state.QueryLength, SocketFlags.None, state.UdpEndpoint, UdpSendCompleted <TMessage>, state);
                state.Timer = new Timer(UdpTimedOut <TMessage>, asyncResult, state.TimeRemaining, Timeout.Infinite);
            }
            catch (Exception e)
            {
                Trace.TraceError("Error on dns query: " + e);

                try
                {
                    state.UdpClient.Close();
                    state.Timer.Dispose();
                }
                catch {}

                state.EndpointInfoIndex++;
                UdpBeginSend(state);
            }
        }
        private void UdpReceiveCompleted <TMessage>(IAsyncResult ar)
            where TMessage : DnsMessageBase, new()
        {
            DnsClientAsyncState <TMessage> state = (DnsClientAsyncState <TMessage>)ar.AsyncState;

            if (state.Timer != null)
            {
                state.Timer.Dispose();
            }

            if (state.TimedOut)
            {
                state.EndpointInfoIndex++;
                UdpBeginSend(state);
            }
            else
            {
                try
                {
                    int    length       = state.UdpClient.EndReceiveFrom(ar, ref state.UdpEndpoint);
                    byte[] responseData = new byte[length];
                    Buffer.BlockCopy(state.Buffer, 0, responseData, 0, length);

                    TMessage response = DnsMessageBase.Parse <TMessage>(responseData, state.TSigKeySelector, state.TSigOriginalMac);

                    if (AreMultipleResponsesAllowedInParallelMode)
                    {
                        if (ValidateResponse(state.Query, response))
                        {
                            if (response.IsTcpResendingRequested)
                            {
                                TcpBeginConnect <TMessage>(state.CreateTcpCloneWithoutCallback(), ((IPEndPoint)state.UdpEndpoint).Address);
                            }
                            else
                            {
                                state.Responses.Add(response);
                            }
                        }

                        state.Buffer = new byte[65535];

                        if (state.EndpointInfos[state.EndpointInfoIndex].IsMulticast)
                        {
                            state.UdpEndpoint = new IPEndPoint(state.UdpClient.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, _port);
                        }

                        IAsyncResult asyncResult = state.UdpClient.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, ref state.UdpEndpoint, UdpReceiveCompleted <TMessage>, state);
                        state.Timer = new Timer(UdpTimedOut <TMessage>, asyncResult, state.TimeRemaining, Timeout.Infinite);
                    }
                    else
                    {
                        state.UdpClient.Close();
                        state.UdpClient   = null;
                        state.UdpEndpoint = null;

                        if (!ValidateResponse(state.Query, response) || (response.ReturnCode == ReturnCode.ServerFailure))
                        {
                            state.EndpointInfoIndex++;
                            UdpBeginSend(state);
                        }
                        else
                        {
                            if (response.IsTcpResendingRequested)
                            {
                                TcpBeginConnect <TMessage>(state, ((IPEndPoint)state.UdpEndpoint).Address);
                            }
                            else
                            {
                                state.Responses.Add(response);
                                state.SetCompleted();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Trace.TraceError("Error on dns query: " + e);

                    try
                    {
                        state.UdpClient.Close();
                        state.Timer.Dispose();
                    }
                    catch {}

                    state.EndpointInfoIndex++;
                    UdpBeginSend(state);
                }
            }
        }
        private void TcpReceiveCompleted <TMessage>(IAsyncResult ar)
            where TMessage : DnsMessageBase, new()
        {
            DnsClientAsyncState <TMessage> state = (DnsClientAsyncState <TMessage>)ar.AsyncState;

            if (state.Timer != null)
            {
                state.Timer.Dispose();
            }

            if (state.TimedOut)
            {
                state.EndpointInfoIndex++;
                TcpBeginConnect(state);
            }
            else
            {
                try
                {
                    state.TcpBytesToReceive -= state.TcpStream.EndRead(ar);

                    if (state.TcpBytesToReceive > 0)
                    {
                        IAsyncResult asyncResult = state.TcpStream.BeginRead(state.Buffer, state.Buffer.Length - state.TcpBytesToReceive, state.TcpBytesToReceive, TcpReceiveCompleted <TMessage>, state);
                        state.Timer = new Timer(TcpTimedOut <TMessage>, asyncResult, state.TimeRemaining, Timeout.Infinite);
                    }
                    else
                    {
                        byte[] buffer = state.Buffer;
                        state.Buffer = null;

                        TMessage response = DnsMessageBase.Parse <TMessage>(buffer, state.TSigKeySelector, state.TSigOriginalMac);

                        if (!ValidateResponse(state.Query, response) || (response.ReturnCode == ReturnCode.ServerFailure))
                        {
                            state.EndpointInfoIndex++;
                            state.PartialMessage = null;
                            state.TcpStream.Close();
                            state.TcpClient.Close();
                            state.TcpStream = null;
                            state.TcpClient = null;
                            TcpBeginConnect(state);
                        }
                        else
                        {
                            bool isSubsequentResponseMessage = (state.PartialMessage != null);

                            if (isSubsequentResponseMessage)
                            {
                                state.PartialMessage.AnswerRecords.AddRange(response.AnswerRecords);
                            }
                            else
                            {
                                state.PartialMessage = response;
                            }

                            if (response.IsTcpNextMessageWaiting(isSubsequentResponseMessage))
                            {
                                state.TcpBytesToReceive = 2;
                                state.Buffer            = new byte[2];

                                IAsyncResult asyncResult = state.TcpStream.BeginRead(state.Buffer, 0, 2, TcpReceiveLengthCompleted <TMessage>, state);
                                state.Timer = new Timer(TcpTimedOut <TMessage>, asyncResult, state.TimeRemaining, Timeout.Infinite);
                            }
                            else
                            {
                                state.TcpStream.Close();
                                state.TcpClient.Close();
                                state.TcpStream = null;
                                state.TcpClient = null;

                                state.Responses.Add(state.PartialMessage);
                                state.SetCompleted();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Trace.TraceError("Error on dns query: " + e);

                    try
                    {
                        state.TcpClient.Close();
                        state.Timer.Dispose();
                    }
                    catch {}

                    state.EndpointInfoIndex++;
                    TcpBeginConnect(state);
                }
            }
        }