public void Dispose()
        {
            if (_isDisposed) return;

            _isDisposed = true;

            // shut down our loopback thread
            if (_loopbackBufferFilledEvent != null)
            {
                _loopbackBufferFilledEvent.Set();
                _loopbackBufferFilledEvent = null;
            }

            _ethernetInterface = null;
            _ipv4HeaderBuffer = null;
            _ipv4HeaderBufferLockObject = null;

            _dhcpv4Client.Dispose();
            _dhcpv4Client = null;

            _icmpv4Handler.Dispose();
            _icmpv4Handler = null;

            _dnsResolver.Dispose();
            _dnsResolver = null;

            _tcpHandler.Dispose();
            _tcpHandler = null;

            _bufferArray = null;
            _indexArray = null;
            _countArray = null;
        }
        public IPv4Layer(EthernetInterface ethernetInterface)
        {
            // save a reference to our Ethernet; we'll use this to push IPv4 frames onto the Ethernet interface
            _ethernetInterface = ethernetInterface;

            // create and configure my ARP resolver; the ARP resolver will automatically wire itself up to receiving incoming ARP frames
            _arpResolver = new ArpResolver(ethernetInterface);

            // retrieve our IP address configuration from the config sector and configure ARP
            object networkInterface = Netduino.IP.Interop.NetworkInterface.GetNetworkInterface(0);
            bool dhcpIpConfigEnabled = (bool)networkInterface.GetType().GetMethod("get_IsDhcpEnabled").Invoke(networkInterface, new object[] { });
            /* NOTE: IsDynamicDnsEnabled is improperly implemented in NETMF; it should implement dynamic DNS--but instead it returns whether or not DNS addresses are assigned through DHCP */
            bool dhcpDnsConfigEnabled = (bool)networkInterface.GetType().GetMethod("get_IsDynamicDnsEnabled").Invoke(networkInterface, new object[] { });

            // randomize our ephemeral port assignment counter (so that we don't use the same port #s repeatedly after reboots)
            _nextEphemeralPort = (UInt16)(FIRST_EPHEMERAL_PORT + ((new Random()).NextDouble() * (UInt16.MaxValue - FIRST_EPHEMERAL_PORT - 1)));

            // configure our ARP resolver's default IP address settings
            if (dhcpIpConfigEnabled)
            {
                // in case of DHCP, temporarily set our IP address to IP_ADDRESS_ANY (0.0.0.0)
                _arpResolver.SetIpv4Address(0);
            }
            else
            {
                _ipv4configIPAddress = ConvertIPAddressStringToUInt32BE((string)networkInterface.GetType().GetMethod("get_IPAddress").Invoke(networkInterface, new object[] { }));
                _ipv4configSubnetMask = ConvertIPAddressStringToUInt32BE((string)networkInterface.GetType().GetMethod("get_SubnetMask").Invoke(networkInterface, new object[] { }));
                _ipv4configGatewayAddress = ConvertIPAddressStringToUInt32BE((string)networkInterface.GetType().GetMethod("get_GatewayAddress").Invoke(networkInterface, new object[] { }));
                _arpResolver.SetIpv4Address(_ipv4configIPAddress);
            }
            // retrieve our DnsServer IP address configuration
            if (!dhcpDnsConfigEnabled)
            {
                string[] dnsAddressesString = (string[])networkInterface.GetType().GetMethod("get_DnsAddresses").Invoke(networkInterface, new object[] { });
                _ipv4configDnsServerAddresses = new UInt32[dnsAddressesString.Length];
                for (int iDnsAddress = 0; iDnsAddress < _ipv4configDnsServerAddresses.Length; iDnsAddress++)
                {
                    _ipv4configDnsServerAddresses[iDnsAddress] = ConvertIPAddressStringToUInt32BE(dnsAddressesString[iDnsAddress]);
                }
            }

            // initialize our buffers
            for (int i = 0; i < _receivedPacketBuffers.Length; i++ )
            {
                _receivedPacketBuffers[i] = new ReceivedPacketBuffer();
                InitializeReceivedPacketBuffer(_receivedPacketBuffers[i]);
            }

            // wire up our IPv4PacketReceived handler
            _ethernetInterface.IPv4PacketReceived += _ethernetInterface_IPv4PacketReceived;
            // wire up our LinkStateChanged event handler
            _ethernetInterface.LinkStateChanged += _ethernetInterface_LinkStateChanged;

            // start our "loopback thread"
            _loopbackThread = new Thread(LoopbackInBackgroundThread);
            _loopbackThread.Start();

            // create our ICMPv4 handler instance
            _icmpv4Handler = new ICMPv4Handler(this);

            // create our DNS resolver instance
            _dnsResolver = new DnsResolver(this);

            // create our DHCP client instance
            _dhcpv4Client = new DHCPv4Client(this);
            _dhcpv4Client.IpConfigChanged += _dhcpv4Client_IpConfigChanged;
            _dhcpv4Client.DnsConfigChanged += _dhcpv4Client_DnsConfigChanged;

            // if we are configured to use DHCP, then create our DHCPv4Client instance now; its state machine will take care of ip configuration from there
            if (dhcpIpConfigEnabled || dhcpDnsConfigEnabled)
            {
                _dhcpv4Client.IsDhcpIpConfigEnabled = dhcpIpConfigEnabled;
                _dhcpv4Client.IsDhcpDnsConfigEnabled = dhcpDnsConfigEnabled;
            }

            // create our TCP handler instance
            _tcpHandler = new TcpHandler(this);

            // manually fire our LinkStateChanged event to set the initial state of our link.
            _ethernetInterface_LinkStateChanged(_ethernetInterface, _ethernetInterface.GetLinkState());
        }