   disconnect: ->
 
     onClosed = (success) =>
-      @transport = null
+      @runtime = null
       @emit 'status',
         online: false
         label: 'disconnected'
       @emit 'disconnected'
 
-    if @transport
-      @transport.close onClosed
+    if @runtime
+      @runtime.transport.close onClosed
     else
       onClosed false
 

  openComm: () ->
    address = @getAddress()

    openFunc = null
    if address.indexOf('serial://') == 0
      serialPort = @getAddress().replace 'serial://', ''
      # TODO: remove hardcoding of baudrate
      baudRate = 9600
      openFunc = (callback) =>
        microflo.serial.openTransport serialPort, baudRate, callback
    else if address.indexOf('simulator://') == 0
      openFunc = (callback) =>
        @simulator = new microflo.simulator.RuntimeSimulator
        @simulator.start()
        @microfloGraph = @simulator.graph # HACK
        @simulator.comm.graph = @simulator.graph
        return callback null, @simulator.transport

    openFunc (err, transport) =>
      @connecting = false
      if err
        console.log 'MicroFlo error:', err
        @emit 'error', err
        return
      @transport = transport

      # Perform capability discovery
      @send 'runtime', 'getruntime',
        secret: @definition.secret

      # FIXME: don't emit connected until we've started FBCS comm
      @emit 'status',
        online: true
        label: 'connected'
      @emit 'connected'

      @flush()

  connect: ->
    return if @connecting
    @connecting = true
    @microfloGraph = {}

    # Make sure serial transport is closed before reopening
    if @transport
      @transport.close () =>
        @transport = null
        @openComm()
    else
      f = () =>

      @buffer.push msg
      return

    try
      @runtime.handleMessage msg
    catch e
      console.log e.stack
      console.log e
