/**
         * Performs the device discovery in API1 or API2 (API Escaped) mode.
         *
         * @param listeners Discovery listeners to be notified about process events.
         * @param id The identifier of the device to be discovered, or {@code null}
         *           to discover all devices in the network.
         *
         * @throws XBeeException if there is an error sending the discovery command.
         */
        private void DiscoverDevicesAPI(IList <IDiscoveryListener> listeners, string id)       /*throws XBeeException */
        {
            if (deviceList == null)
            {
                deviceList = new List <RemoteXBeeDevice>();
            }
            deviceList.Clear();

            IPacketReceiveListener packetReceiveListener = new CustomPacketReceiveListener(this, listeners, id);

            logger.DebugFormat("{0}Start listening.", xbeeDevice.ToString());
            xbeeDevice.AddPacketListener(packetReceiveListener);

            try
            {
                long deadLine  = 0;
                var  stopwatch = Stopwatch.StartNew();

                // In 802.15.4 devices, the discovery finishes when the 'end' command
                // is received, so it's not necessary to calculate the timeout.
                if (xbeeDevice.XBeeProtocol != XBeeProtocol.RAW_802_15_4)
                {
                    deadLine += CalculateTimeout(listeners);
                }

                sendNodeDiscoverCommand(id);

                if (xbeeDevice.XBeeProtocol != XBeeProtocol.RAW_802_15_4)
                {
                    // Wait for scan timeout.
                    while (discovering)
                    {
                        if (stopwatch.ElapsedMilliseconds < deadLine)
                        {
                            try
                            {
                                Thread.Sleep(100);
                            }
                            catch (ThreadInterruptedException) { }
                        }
                        else
                        {
                            stopwatch.Stop();
                            discovering = false;
                        }
                    }
                }
                else
                {
                    // Wait until the 'end' command is received.
                    while (discovering)
                    {
                        try
                        {
                            Thread.Sleep(100);
                        }
                        catch (ThreadInterruptedException) { }
                    }
                }
            }
            finally
            {
                xbeeDevice.RemovePacketListener(packetReceiveListener);
                logger.DebugFormat("{0}Stop listening.", xbeeDevice.ToString());
            }
        }
		/**
		 * Performs the device discovery in API1 or API2 (API Escaped) mode.
		 * 
		 * @param listeners Discovery listeners to be notified about process events.
		 * @param id The identifier of the device to be discovered, or {@code null}
		 *           to discover all devices in the network.
		 * 
		 * @throws XBeeException if there is an error sending the discovery command.
		 */
		private void DiscoverDevicesAPI(IList<IDiscoveryListener> listeners, string id)/*throws XBeeException */{
			if (deviceList == null)
				deviceList = new List<RemoteXBeeDevice>();
			deviceList.Clear();

			IPacketReceiveListener packetReceiveListener = new CustomPacketReceiveListener(this, listeners, id);

			logger.DebugFormat("{0}Start listening.", xbeeDevice.ToString());
			xbeeDevice.AddPacketListener(packetReceiveListener);

			try
			{
				long deadLine = 0;
				var stopwatch = Stopwatch.StartNew();

				// In 802.15.4 devices, the discovery finishes when the 'end' command 
				// is received, so it's not necessary to calculate the timeout.
				if (xbeeDevice.XBeeProtocol != XBeeProtocol.RAW_802_15_4)
					deadLine += CalculateTimeout(listeners);

				sendNodeDiscoverCommand(id);

				if (xbeeDevice.XBeeProtocol != XBeeProtocol.RAW_802_15_4)
				{
					// Wait for scan timeout.
					while (discovering)
					{
						if (stopwatch.ElapsedMilliseconds < deadLine)
							try
							{
								Thread.Sleep(100);
							}
							catch (ThreadInterruptedException ) { }
						else
						{
							stopwatch.Stop();
							discovering = false;
						}
					}
				}
				else
				{
					// Wait until the 'end' command is received.
					while (discovering)
					{
						try
						{
							Thread.Sleep(100);
						}
						catch (ThreadInterruptedException ) { }
					}
				}
			}
			finally
			{
				xbeeDevice.RemovePacketListener(packetReceiveListener);
				logger.DebugFormat("{0}Stop listening.", xbeeDevice.ToString());
			}
		}