/**
         * Utility function to join an existing network as a Router
         *
         * @param networkParameters the required {@link EmberNetworkParameters}
         * @param linkKey the {@link ZigBeeKey} with the initial link key. This cannot be set to all 00 or all FF.
         */
        public void JoinNetwork(EmberNetworkParameters networkParameters, ZigBeeKey linkKey)
        {
            Log.Debug("Joining Ember network with configuration {Parameters}", networkParameters);

            // Leave the current network so we can initialise a new network
            EmberNcp ncp = new EmberNcp(_protocolHandler);

            if (CheckNetworkJoined())
            {
                ncp.LeaveNetwork();
            }

            ncp.ClearKeyTable();

            // Initialise security - no network key as we'll get that from the coordinator
            SetSecurityState(linkKey, null);

            DoJoinNetwork(networkParameters);
        }
        /**
         * This utility function uses emberStartScan, emberStopScan, emberScanCompleteHandler, emberEnergyScanResultHandler,
         * and emberNetworkFoundHandler to discover other networks or determine the background noise level. It then uses
         * emberFormNetwork to create a new network with a unique PAN-ID on a channel with low background noise.
         * <p>
         * Setting the PAN-ID or Extended PAN-ID to 0 will set these values to a random value.
         * <p>
         * If channel is set to 0, the quietest channel will be used.
         *
         * @param networkParameters the required {@link EmberNetworkParameters}
         * @param linkKey the {@link ZigBeeKey} with the link key. This can not be set to all 00 or all FF.
         * @param networkKey the {@link ZigBeeKey} with the network key. This can not be set to all 00 or all FF.
         */
        public void FormNetwork(EmberNetworkParameters networkParameters, ZigBeeKey linkKey, ZigBeeKey networkKey)
        {
            if (networkParameters.GetExtendedPanId() == null)
            {
                networkParameters.SetExtendedPanId(new ExtendedPanId());
            }

            Log.Debug("Initialising Ember network with configuration {NetworkParameters}", networkParameters);

            EmberNcp ncp = new EmberNcp(_protocolHandler);

            // Leave the current network so we can initialise a new network
            if (CheckNetworkJoined())
            {
                ncp.LeaveNetwork();
            }

            ncp.ClearKeyTable();

            // Perform an energy scan to find a clear channel
            int?quietestChannel = DoEnergyScan(ncp, _scanDuration);

            Log.Debug("Energy scan reports quietest channel is {QuietestChannel}", quietestChannel);

            // Check if any current networks were found and avoid those channels, PAN ID and especially Extended PAN ID
            ncp.DoActiveScan(ZigBeeChannelMask.CHANNEL_MASK_2GHZ, _scanDuration);

            // Read the current network parameters
            GetNetworkParameters();

            // Create a random PAN ID and Extended PAN ID
            if (networkParameters.GetPanId() == 0 || networkParameters.GetExtendedPanId().Equals(new ExtendedPanId()))
            {
                Random random = new Random();
                int    panId  = random.Next(65535);
                networkParameters.SetPanId(panId);
                Log.Debug("Created random PAN ID: {PanId}", panId);

                byte[] extendedPanIdBytes = new byte[8];
                random.NextBytes(extendedPanIdBytes);
                ExtendedPanId extendedPanId = new ExtendedPanId(extendedPanIdBytes);
                networkParameters.SetExtendedPanId(extendedPanId);
                Log.Debug("Created random Extended PAN ID: {ExtendedPanId}", extendedPanId.ToString());
            }

            if (networkParameters.GetRadioChannel() == 0 && quietestChannel.HasValue)
            {
                networkParameters.SetRadioChannel(quietestChannel.Value);
            }

            // If the channel set is empty, use the single channel defined above
            if (networkParameters.GetChannels() == 0)
            {
                networkParameters.SetChannels(1 << networkParameters.GetRadioChannel());
            }

            // Initialise security
            SetSecurityState(linkKey, networkKey);

            // And now form the network
            DoFormNetwork(networkParameters);
        }