/// <summary>
 /// This method invokes the GPO reader, and then sets Immediate Access's internal timers.
 /// </summary>
 private static void UpdatePolicy()
 {
     PolicyReader.ReadPolicies();
     HealthCheckTimer.Interval = (int)PolicyReader.Policies["HealthCheckIntervalS"] * 1000;
     HealthCheckTimer.Start();
     NetEventCoolTimer.Interval = (int)PolicyReader.Policies["NetEventCooldownS"] * 1000;
     NetEventCoolTimer.Stop();
 }
        /// <summary>
        /// This method contains the main logic for the Immediate Access service. In a nutshell, this method tests if the probe is available, and if it is not,
        /// selects and then connects to a VPN profile.
        /// </summary>
        /// <returns>Task: to enable threading of this method to allow for canceling.</returns>
        private static async Task EnsureConnectionToIntranet()
        {
            try
            {
                UpdatePolicy();
                if (IsCurrentlyEnsuring || !IsNetworkAvailable || !PolicyReader.IsServiceEnabled())
                {
                    return;
                }
                EnsuranceCancel     = new CancellationTokenSource();
                IsCurrentlyEnsuring = true;
                if (await TestNetwork.IsProbeAvailable())
                {
                    IsCurrentlyEnsuring = false;
                    await VpnControl.Disconnect();

                    return;
                }
                if (await VpnControl.IsConnected() != null)
                {
                    IsCurrentlyEnsuring = false;
                    return;
                }
                EnsuranceCancel.Token.ThrowIfCancellationRequested();
                int attempts = (int)PolicyReader.Policies["VpnServerConnectAttempts"];
                while (attempts-- > 0)
                {
                    EnsuranceCancel.Token.ThrowIfCancellationRequested();
                    if (!await TestNetwork.SelectOnlineVpnProfile())
                    {
                        IsCurrentlyEnsuring = false;
                        return;
                    }
                    if (await VpnControl.Connect(EnsuranceCancel.Token))
                    {
                        IsCurrentlyEnsuring = false;
                        _ = EnsureConnectionToIntranet();
                        return;
                    }
                    if (attempts > 0)
                    {
                        Logger.Warning("Couldn't connect to VPN for some reason. Trying again in 5 seconds...");
                        await Task.Delay(5000);
                    }
                }
                IsCurrentlyEnsuring = false;
            }
            catch (OperationCanceledException)
            {
                IsCurrentlyEnsuring = false;
                _ = EnsureConnectionToIntranet();
            }
        }