       @buffer.push msg
       return
 
-    sendFunc = (response) =>
-      @onMessage { data: response }
-    conn = { send: sendFunc }
     try
-      microflo.runtime.handleMessage msg, conn, @microfloGraph, @transport, @debugLevel
+      @runtime.handleMessage msg
     catch e
       console.log e.stack
       console.log e

  disconnect: ->

    onClosed = (success) =>
      @transport = null
      @emit 'status',
        online: false
        label: 'disconnected'
      @emit 'disconnected'

    if @transport
      @transport.close onClosed
    else
      onClosed false


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

    getRuntime = null
    if address.indexOf('serial://') == 0
      serialPort = @getAddress().replace 'serial://', ''
      # TODO: remove hardcoding of baudrate
      baudRate = 9600
      getRuntime = (callback) =>
        microflo.serial.openTransport serialPort, baudRate, (err, transport) ->
          return callback err if err
          dev = new microflo.runtime.Runtime transport
          return callback null, dev
    else if address.indexOf('simulator://') == 0
      getRuntime = (callback) =>
        sim = new microflo.simulator.RuntimeSimulator
        sim.start()
        #sim.device.graph = sim.graph
        return callback null, sim

    getRuntime (err, runtime) =>
      return @emit 'error', err if err

      runtime.on 'message', (response) =>
        @onMessage { data: response }

      runtime.device.open () =>
        @connecting = false
        if err
          console.log 'MicroFlo error:', err
          @emit 'error', err
          return
        @runtime = runtime

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

        @emit 'status',
          online: true
          label: 'connected'
        @emit 'connected'

        @flush()

  connect: ->
    return if @connecting
    @connecting = true

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