private void RegisterCallback(IntPtr handle, Action <RasConnectionEventArgs> callback, RASCN changeNotification)
        {
            var stateObject = new RasConnectionNotificationStateObject();
            IRegisteredCallback registeredCallback = null;

            try
            {
                registeredCallback = callbackFactory.Create(callbackHandler.OnCallback, stateObject);
                if (registeredCallback == null)
                {
                    throw new InvalidOperationException("The factory did not register a callback.");
                }

                var ret = rasApi32.RasConnectionNotification(handle, registeredCallback.Handle, changeNotification);
                if (ret != SUCCESS)
                {
                    throw exceptionPolicy.Create(ret);
                }

                stateObject.RegisteredCallback = registeredCallback;
                stateObject.Callback           = callback;
                stateObject.NotificationType   = changeNotification;

                notifications.Add(changeNotification, stateObject);
            }
            catch (Exception)
            {
                registeredCallback?.Dispose();
                throw;
            }
        }
        private void OnCallback(RasConnectionNotificationStateObject state)
        {
            lock (syncRoot)
            {
                var current = enumConnectionsService.EnumerateConnections().ToArray();

                var changes = FindConnectionChanges(current);
                if (changes.Any())
                {
                    ExecuteCallbackForChanges(state.Callback, changes);
                }

                previousState = current;
            }
        }