   'index.css'
 ]
 
+log = (s) -> console.info process.uptime().toFixed(3) + ' ' + s
+
 js = css = ''
 do jsPrepare = ->
   js = jsFiles.map((f) -> s = fs.readFileSync f, 'utf8'; if /\.coffee$/.test f then coffee.compile s else s).join '\n'
-  console.info "prepared js: #{js.length} bytes"
+  log "prepared js: #{js.length} bytes"
 do cssPrepare = ->
   css = cssFiles.map((f) -> fs.readFileSync f, 'utf8').join '\n'
-  console.info "prepared css: #{css.length} bytes"
+  log "prepared css: #{css.length} bytes"
 jsFiles.forEach (f) -> fs.watch f, jsPrepare
 cssFiles.forEach (f) -> fs.watch f, cssPrepare
 

app.get '/D.css', (req, res) -> res.header('Content-Type', 'text/css').send css

app.use '/', router
server = app.listen 3000, -> console.info 'HTTP server listening on :3000'

io.listen(server).on 'connection', (socket) ->
  console.info 'browser connected'
  client = net.connect {host: '127.0.0.1', port: 4502}, -> console.info 'interpreter connected'

  toInterpreter = (s) ->
    console.info 'to interpreter: ' + JSON.stringify(s)[..1000]
    console.assert /[\x01-\x7f]*/.test s
    b = Buffer s.length + 8
    b.writeInt32BE b.length, 0
    b.write 'RIDE' + s, 4
    client.write b

  toBrowser = (m...) -> console.info 'to browser: ' + JSON.stringify(m)[..1000]; socket.emit m...

  q = Buffer 0 # a buffer for data received from the server
  client.on 'data', (data) ->
    q = Buffer.concat [q, data]
    while q.length >= 4 and (n = q.readInt32BE 0) <= q.length
      m = '' + q[8...n]; q = q[n..]
      console.info 'from interpreter: ' + JSON.stringify(m)[..1000]
      if !/^(?:SupportedProtocols|UsingProtocol)=1$/.test m # ignore these
        switch (/^<(\w+)>/.exec m)?[1] or ''
          when 'ReplyIdentify', 'ReplyConnect', 'ReplyNotAtInputPrompt', 'ReplyEdit', 'ReplySaveChanges' then ; # ignore

  toInterpreter '<?xml version="1.0" encoding="utf-8"?><Command><cmd>Identify</cmd><id>0</id><args><Identify><Sender><Process>RIDE.EXE</Process><Proxy>0</Proxy></Sender></Identify></args></Command>'
  toInterpreter '<?xml version="1.0" encoding="utf-8"?><Command><cmd>Connect</cmd><id>0</id><args><Connect><Token /></Connect></args></Command>'

  client.on 'end', -> log 'interpreter disconnected'; socket.emit 'end'
  socket.on 'disconnect', -> log 'browser disconnected'; client.end()
