/// <summary>
        /// Query a list of filtered alarm lines and the statistics for all alarms on this server
        /// </summary>
        /// <returns></returns>
        private bool GetAlarmData()
        {
            // We need an array of alarm lines to receive data to. An alarm line is the "short version" of an alarm
            try
            {
                // We want to have a rolling window always showing the 10 newest alarms matching the filter
                // That is ok for a sample program playing Xmas songs. Your application probably needs to be somewhat smarter.
                // We also want to get some general statistics

                if (_isBasicUser)
                {
                    _alarms = _alarmCommandTokenClient.GetAlarmLines(_token, 0, 10, _filter);
                    _stats  = _alarmCommandTokenClient.GetStatistics(_token);
                }
                else
                {
                    _alarms = _alarmCommandClient.GetAlarmLines(0, 10, _filter);
                    _stats  = _alarmCommandClient.GetStatistics();
                }

                // We need to update the UI in the UI thread. Please note that if you want to access the list view from other threads also, you must provide the thread safety.
                if (_alarms != null)
                {
                    Dispatcher.Invoke(_onAlarmsReceived);
                }
                // We need to update the UI in the UI thread. Please note that if you want to access the list view from other threads also, you must provide the thread safety.
                if (_stats != null)
                {
                    Dispatcher.Invoke(_onStatsReceived);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Exception");
                ResetUiDelegate resetUiDelegate = new ResetUiDelegate(resetUi);
                _buttonStop.Dispatcher.Invoke(resetUiDelegate);
                return(false);
            }

            // If one of the alarms is all new (newer than latest new alarm), sound the alarm
            if (_alarms != null && _alarms.Length > 0)
            {
                if (_alarms[0].Timestamp > _lastUsedTimestamp)
                {
                    _lastUsedTimestamp = _alarms[0].Timestamp;
                    SoundTheAlarm();
                }
            }
            return(true);
        }
        /// <summary>
        /// Creates and opens an instance of the WSDL generated client from AlarmCommandToken.cs or AlarmCommand.cs
        /// </summary>
        /// <returns></returns>
        private bool OpenClient()
        {
            try
            {
                int port = 22331;
                if (_isBasicUser)
                {
                    BasicHttpBinding binding = new BasicHttpBinding();
                    binding.MaxReceivedMessageSize              = MESSAGE_BUFFER_SIZE;
                    binding.MaxBufferSize                       = MESSAGE_BUFFER_SIZE;
                    binding.MaxBufferPoolSize                   = MESSAGE_BUFFER_SIZE;
                    binding.ReaderQuotas.MaxArrayLength         = MESSAGE_BUFFER_SIZE;
                    binding.ReaderQuotas.MaxBytesPerRead        = MESSAGE_BUFFER_SIZE;
                    binding.ReaderQuotas.MaxNameTableCharCount  = MESSAGE_BUFFER_SIZE;
                    binding.ReaderQuotas.MaxDepth               = MESSAGE_BUFFER_SIZE;
                    binding.ReaderQuotas.MaxStringContentLength = MESSAGE_BUFFER_SIZE;

                    Uri    uri             = new UriBuilder(Uri.UriSchemeHttp, _hostName, port, "Central/AlarmServiceToken").Uri;
                    string alarmServiceUri = uri.AbsoluteUri;
                    var    endpointAddress = new EndpointAddress(alarmServiceUri);

                    _alarmCommandTokenClient = new AlarmCommandTokenClient(binding, endpointAddress);

                    try
                    {
                        _alarmCommandTokenClient.Open();
                        if (!(_alarmCommandTokenClient.State != CommunicationState.Opening ||
                              _alarmCommandTokenClient.State != CommunicationState.Opened))
                        {
                            ResetUiDelegate resetUiDelegate = new ResetUiDelegate(resetUi);
                            _buttonStop.Dispatcher.Invoke(resetUiDelegate);
                            return(false);
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Error opening AlarmCommandTokenClient: " + e.Message + ((e.InnerException != null) ? (" - " + e.InnerException.Message) : ""));
                        ResetUiDelegate resetUiDelegate = new ResetUiDelegate(resetUi);
                        _buttonStop.Dispatcher.Invoke(resetUiDelegate);
                        return(false);
                    }
                }
                else
                {
                    WSHttpBinding binding = new WSHttpBinding(SecurityMode.None);
                    binding.MaxReceivedMessageSize              = MESSAGE_BUFFER_SIZE;
                    binding.MaxBufferPoolSize                   = MESSAGE_BUFFER_SIZE;
                    binding.ReaderQuotas.MaxArrayLength         = MESSAGE_BUFFER_SIZE;
                    binding.ReaderQuotas.MaxBytesPerRead        = MESSAGE_BUFFER_SIZE;
                    binding.ReaderQuotas.MaxNameTableCharCount  = MESSAGE_BUFFER_SIZE;
                    binding.ReaderQuotas.MaxDepth               = MESSAGE_BUFFER_SIZE;
                    binding.ReaderQuotas.MaxStringContentLength = MESSAGE_BUFFER_SIZE;
                    binding.Security.Mode = SecurityMode.Message;
                    binding.Security.Transport.ClientCredentialType     = HttpClientCredentialType.Windows;
                    binding.Security.Transport.ProxyCredentialType      = HttpProxyCredentialType.None;
                    binding.Security.Message.ClientCredentialType       = MessageCredentialType.Windows;
                    binding.Security.Message.EstablishSecurityContext   = false;
                    binding.Security.Message.NegotiateServiceCredential = true;
                    var uri             = new UriBuilder(Uri.UriSchemeHttp, _hostName, port, "/Central/AlarmService2").Uri;
                    var endpointAddress = new EndpointAddress(uri, EndpointIdentity.CreateSpnIdentity(BasicConnection.SpnFactory.GetSpn(uri)));

                    _alarmCommandClient = new AlarmCommandClient(binding, endpointAddress);
                    _alarmCommandClient.ClientCredentials.Windows.ClientCredential.UserName = _userName;
                    _alarmCommandClient.ClientCredentials.Windows.ClientCredential.Password = _password;
                    try
                    {
                        _alarmCommandClient.Open();
                        if (!(_alarmCommandClient.State != CommunicationState.Opening ||
                              _alarmCommandClient.State != CommunicationState.Opened))
                        {
                            ResetUiDelegate resetUiDelegate = new ResetUiDelegate(resetUi);
                            _buttonStop.Dispatcher.Invoke(resetUiDelegate);
                            return(false);
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Error opening AlarmCommandClient: " + e.Message + ((e.InnerException != null) ? (" - " + e.InnerException.Message) : ""));
                        ResetUiDelegate resetUiDelegate = new ResetUiDelegate(resetUi);
                        _buttonStop.Dispatcher.Invoke(resetUiDelegate);
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error opening Client: " + e.Message + ((e.InnerException != null) ? (" - " + e.InnerException.Message) : ""), "Exception");
                ResetUiDelegate resetUiDelegate = new ResetUiDelegate(resetUi);
                _buttonStop.Dispatcher.Invoke(resetUiDelegate);
                return(false);
            }
            return(true);
        }