/// <summary>
        /// Connects to MCS Live Server.
        /// </summary>
        public bool Connect()
        {
            Disconnect();  // Remove any existing connection objects
            string received;
            string url = string.Format(_addressDeviceLiveConnect, _MID);

            if (Get(url, out received))
            {
                if (received != null && received.Length > 0)
                {
                    LiveConnectionDetails connectionDetails = JsonConvert.DeserializeObject <LiveConnectionDetails>(received);
                    if (connectionDetails != null)
                    {
                        // Connect to messaging server.
                        if (Open(connectionDetails))
                        {
                            // Enable automatic callback handler if specified.
                            if (UseAutomaticCallbacks)
                            {
                                lock (_modelLock)
                                {
                                    _consumer           = new EventingBasicConsumer(_model);
                                    _consumer.Received += ConsumerReceived;
                                    _consumerTag        = _model.BasicConsume(_rmqQueueName, false, _consumer);
                                }
                            }
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
        private bool Open(LiveConnectionDetails connectionDetails)
        {
            if (connectionDetails.UseSsl)
            {
                var ssl = new SslOption();
                ssl.Enabled = true;
                if (IgnoreSslCertificateErrors)
                {
                    ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNotAvailable | SslPolicyErrors.RemoteCertificateNameMismatch | SslPolicyErrors.RemoteCertificateChainErrors;
                }
                ssl.ServerName     = connectionDetails.Server;
                _connectionFactory = new ConnectionFactory
                {
                    HostName           = connectionDetails.Server,
                    UserName           = _MID,
                    Password           = _preSharedKey,
                    RequestedHeartbeat = new TimeSpan(0, 0, _heartbeatInterval),
                    Ssl = ssl,
                };
            }
            else
            {
                _connectionFactory = new ConnectionFactory
                {
                    HostName           = connectionDetails.Server,
                    UserName           = _MID,
                    Password           = _preSharedKey,
                    RequestedHeartbeat = new TimeSpan(0, 0, _heartbeatInterval)
                };
            }

            try
            {
                _connection = _connectionFactory.CreateConnection();

                if (_connection != null && _connection.IsOpen)
                {
                    lock (_modelLock)
                    {
                        _model = _connection.CreateModel();
                        _model.BasicQos(0, (ushort)this.PrefetchCount, false);
                        _transactionOpen = false;
                        return(_model != null && _model.IsOpen);
                    }
                }
            }
            catch (Exception e)
            {
                LastErrorMessage = e.Message;
            }
            return(false);  // Failed to create connection
        }
        protected static MasterloopLiveConnection GetMCSLiveTemporary()
        {
            MasterloopServerConnection mcs = GetMCSAPI();
            LiveAppRequest             lar = new LiveAppRequest()
            {
                MID = GetMID(),
                ConnectAllCommands     = true,
                ConnectAllObservations = true,
                InitObservationValues  = false,
                ReceiveDevicePulse     = true
            };
            LiveConnectionDetails lcd = mcs.RequestLiveConnection(new LiveAppRequest[] { lar });

            return(new MasterloopLiveConnection(lcd));
        }
        protected static MasterloopLiveConnection GetMCSPersistentConnection()
        {
            MasterloopServerConnection mcs = GetMCSAPI();
            string subscriptionKey         = GetPersistentSubscriptionKey();

            mcs.DeleteLivePersistentSubscription(subscriptionKey);
            LivePersistentSubscriptionRequest request = new LivePersistentSubscriptionRequest()
            {
                SubscriptionKey    = subscriptionKey,
                TID                = GetTID(),
                ConnectAllCommands = true
            };

            if (mcs.CreateLivePersistentSubscription(request))
            {
                LiveConnectionDetails lcd = mcs.GetLivePersistentSubscriptionConnection(subscriptionKey);
                return(new MasterloopLiveConnection(lcd));
            }
            else
            {
                throw new Exception("Could not create subscription");
            }
        }