//=================
        #endregion

        #region Communication
        //=================

        public void Connect()
        {
            if (this.Connection != null)
            {
                throw new InvalidOperationException("ServiceInstance is already connected.");
            }

            if (String.IsNullOrEmpty(this.Configuration.HostName))
            {
                throw new ServiceException("Configuration.HostName cannot be null.");
            }

            // Get a connection
            try { this.Connection = Environment.AcquireHostConnection(this.Configuration.HostName, this.InstanceID); }
            catch (Exception ex)
            {
                throw new ServiceException(String.Format("Environment could not acquire a connection to the service {0:N} on host {1}.", InstanceID, Configuration.HostName), ex);
            }

            // Callback
            this.Connection.StateChangedCallback    = OnStateChanged;
            this.Connection.OutputGeneratedCallback = OnOutputGenerated;

            try { this.Connection.RefreshState(); }
            catch (Exception ex)
            {
                throw new ServiceException(String.Format("Instance {0:N} failed to communicte with host {1}.", InstanceID, Configuration.HostName), ex);
            }
        }