/**
   * Constructor.
   *
   * @param listener Sdl connection listener.
   * @param transportConfig Transport configuration for this connection.
   */
  public SdlConnection(BaseTransportConfig transportConfig) {
    _connectionListener = new InternalMsgDispatcher();

    // Initialize the transport
    synchronized (TRANSPORT_REFERENCE_LOCK) {
      // Ensure transport is null
      if (_transport != null) {
        if (_transport.getIsConnected()) {
          _transport.disconnect();
        }
        _transport = null;
      }

      if (transportConfig.getTransportType() == TransportType.BLUETOOTH) {
        BTTransportConfig myConfig = (BTTransportConfig) transportConfig;
        _transport = new BTTransport(this, myConfig.getKeepSocketActive());
      } else if (transportConfig.getTransportType() == TransportType.TCP) {
        _transport = new TCPTransport((TCPTransportConfig) transportConfig, this);
      } else if (transportConfig.getTransportType() == TransportType.USB) {
        _transport = new USBTransport((USBTransportConfig) transportConfig, this);
      }
    }

    // Initialize the protocol
    synchronized (PROTOCOL_REFERENCE_LOCK) {
      // Ensure protocol is null
      if (_protocol != null) {
        _protocol = null;
      }

      _protocol = new WiProProtocol(this);
    }
  }
  private void closeConnection(boolean willRecycle, byte rpcSessionID) {
    synchronized (PROTOCOL_REFERENCE_LOCK) {
      if (_protocol != null) {
        // If transport is still connected, sent EndProtocolSessionMessage
        if (_transport != null && _transport.getIsConnected()) {
          _protocol.EndProtocolSession(SessionType.RPC, rpcSessionID);
        }
        if (willRecycle) {
          _protocol = null;
        }
      } // end-if
    }

    synchronized (TRANSPORT_REFERENCE_LOCK) {
      if (willRecycle) {
        if (_transport != null) {
          _transport.disconnect();
        }
        _transport = null;
      }
    }
  }