private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
#if TRACE
            LogExceptions.LogTrace(true, "NetworkChange_NetworkAddressChanged");
#endif
            AutoUpdatedSharedConnection();
        }
        private void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
#if TRACE
            LogExceptions.LogTrace(true, "NetworkChange_NetworkAvailabilityChanged>" + e.IsAvailable);
#endif
            AutoUpdatedSharedConnection();
        }
        private void AutoUpdatedSharedConnection()
        {
            if (_status != WorkingStatus.Started)
            {
                // only when the app is started this should update the shared connection!
                return;
            }

            if (!ConfigShareInternet)
            {
                return;
            }
            if (!ConfigAutoInternet)
            {
                return;
            }

            var internetConnected = GetIcsToShare();

            if (internetConnected == null)
            {
                return;
            }

            // connection is changed
            if (_sharedConnection == null || internetConnected.Guid != _sharedConnection.Guid)
            {
#if TRACE
                LogExceptions.LogTrace(true, "AutoUpdatedSharedConnection -> " + internetConnected.Name);
#endif

                ShareConnection(internetConnected);
            }
        }
        private void DisableSharing(bool force = false)
        {
            if (!force)
            {
                if (_sharedConnection == null)
                {
#if TRACE
                    LogExceptions.LogTrace(true, "WLanDisableSharing,NotShared,forced?=" + force);
#endif
                    return;
                }
#if TRACE
                LogExceptions.LogTrace(true, "WLanDisableSharing,Shared,forced?=" + force);
#endif
            }
            else
            {
#if TRACE
                LogExceptions.LogTrace(true, "WLanDisableSharing,NoState,forced?=" + force);
#endif
            }
            if (_icsManager.SharingInstalled)
            {
                _icsManager.DisableIcsOnAll();
            }
            SharedConnection = null;
        }
        private bool StopInternal(bool verbose)
        {
            try
            {
                Status = WorkingStatus.Stopping;

                DisableSharing(true);

                _wlanManager.StopHostedNetwork();

#if TRACE
                LogExceptions.LogTrace(true, "WLanStop->Done");
#endif
                if (verbose)
                {
                    Status = WorkingStatus.Stopped;
                }

                return(true);
            }
            catch (Exception ex)
            {
                LogExceptions.Log(ex);

                Status = WorkingStatus.StopFailed;
                return(false);
            }
        }
        private void StartInternal()
        {
            try
            {
                StopInternal(false);

                Status = WorkingStatus.Starting;

                // system fixes
                SystemTweak.TweakTheSystemNotForced();

                // setting the options
                _wlanManager.SetConnectionSettings(ConfigSsid, ConfigMaxPeers);
                _wlanManager.SetSecondaryKey(ConfigPassword);

                _wlanManager.StartHostedNetwork();

                // save the network id
                _hostedNetworkInterfaceGuid = _wlanManager.HostedNetworkInterfaceGuid;

                var connection = GetIcsToShare();

                if (connection != null)
                {
                    ShareConnection(connection);
                }
                else
                {
                    DisableSharing(true);
                }

                Status = WorkingStatus.Started;
#if TRACE
                LogExceptions.LogTrace(true,
                                       "WLanStart completed, internet is: " + ((connection == null) ? "NULL" : connection.Name));
#endif
            }
            catch (Exception ex)
            {
                LogExceptions.Log(ex);
                Status = WorkingStatus.StartFailed;
            }
        }
        private void ShareConnection(IcsConnection icsConnection)
        {
            // disable sharing first
            DisableSharing(false);

            if (icsConnection == null)
            {
                return;
            }

#if TRACE
            LogExceptions.LogTrace(true, "ShareConnection -> " + icsConnection.Name);
#endif
            Exception failedToSharException = null;

            // then enabled the sharing
            for (int i = 1; i < 10; i++)
            {
                try
                {
                    if (_wlanManager.HostedNetworkState != WLAN_HOSTED_NETWORK_STATE.wlan_hosted_network_active)
                    {
                        Thread.Sleep(500);
                        continue;
                    }

                    var privateConnectionGuid = _wlanManager.HostedNetworkInterfaceGuid;

                    _icsManager.EnableIcs(icsConnection.Guid, privateConnectionGuid);

                    // the connected network guid to internet
                    SharedConnection = icsConnection;

                    failedToSharException = null;
#if TRACE
                    LogExceptions.LogTrace(true, "ShareConnection.EnableIcs on try no:" + i);
#endif
                    return;
                }
                catch (Exception ex)
                {
#if DEBUG
                    LogExceptions.Log(ex);
#endif
                    failedToSharException = ex;
                    Thread.Sleep(500);
                }
            }

            // if we have reached here that means the connection is failed to share with many tries
            if (failedToSharException != null)
            {
#if TRACE
                LogExceptions.LogTrace(true, "ShareConnection is failed -> " + icsConnection.Name,
                                       failedToSharException.ToString());
#endif
                RaiseOnFailedToEnableSharing(icsConnection, failedToSharException);
            }
            else
            {
#if TRACE
                LogExceptions.LogTrace(true, "ShareConnection is failed -> " + icsConnection.Name);
#endif
            }
        }