protected virtual bool check_wanted_packet(ref ipv4_header iph)
 {
     if (this.spyed_protocol == ipv4_header_server.PROTOCOL_ALL)
     {
         return(true);
     }
     return(iph.protocol == this.spyed_protocol);
 }
        protected void thread_watching_reply()
        {
            int nBytes;

            byte[] ReceiveBuffer = new byte[this.us_receive_buffer_size];

            ipv4_header iph = new ipv4_header();

            byte[] data_buffer;
            byte   b_ret;

            try
            {
                while (true)
                {
                    nBytes = this.srv_socket.Receive(ReceiveBuffer, 0, this.us_receive_buffer_size, System.Net.Sockets.SocketFlags.None);
                    if (nBytes > 0)
                    {
                        // allow to have an array matching data rcv size (avoid to send the size as parameter for each next methodes)
                        data_buffer = new byte[nBytes];
                        System.Array.Copy(ReceiveBuffer, 0, data_buffer, 0, nBytes);
                        // end allow to have an array matching data rcv size (avoid to send the size as parameter for each next methodes)

                        // decode ipheader
                        b_ret = iph.decode(data_buffer, nBytes);
                        // if decoding was successful
                        if (b_ret == ipv4_header.error_success)
                        {
                            if (this.check_wanted_packet(ref iph))
                            {
                                // throw the data arrival event
                                if (this.event_Socket_Data_Arrival != null)
                                {
#if (!DEBUG)
                                    try // avoid to close server if error in the event handler code
#endif
                                    {
                                        this.event_Socket_Data_Arrival(iph, new EventArgs_FullPacket(data_buffer));
                                    }
#if (!DEBUG)
                                    catch {}
#endif
                                }
                            }
                        }
                        else
                        {
                            if ((b_ret == ipv4_header.error_datagram_internet_header_length_too_small) ||
                                (b_ret == ipv4_header.error_datagram_total_length_too_small))
                            {
                                // send
                                if (this.event_Socket_RcvPacket_Error != null)
                                {
                                    this.event_Socket_RcvPacket_Error(this, new EventArgs_Exception_Packet(new Exception("Error in Ipv4 header"), data_buffer));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                // close socket
                this.close_socket();


                if (e is System.Net.Sockets.SocketException)
                {
                    System.Net.Sockets.SocketException se = (System.Net.Sockets.SocketException)e;
                    if (se.ErrorCode == 10004) // close methode as been called
                    {
                        return;
                    }
                }
                if (e is System.ObjectDisposedException)//throw by Receive (socket has been closed)
                {
                    return;
                }
                if (e is System.NullReferenceException)//throw by Receive if we put socket=null after socket.close
                {
                    return;
                }
                // send event_Socket_Server_Error

                //if (this.event_Socket_Error!=null)
                //    this.event_Socket_Error(iph,new EventArgs_Exception(e));

                this.send_socket_error_event(e);
            }
        }