 
 	_onRead: (readInfo) =>
 		console.log "onRead", readInfo
+		@_active() unless readInfo.resultCode is -1
 		if readInfo.resultCode < -1 # -1 is EWOULDBLOCK
 			@emit 'error', readInfo.resultCode
 		else if readInfo.resultCode is 0

		@listeners = {}
	on: (ev, cb) ->
		(@listeners[ev] ?= []).push cb
	emit: (ev, args...) ->
		l(args...) for l in (@listeners[ev] ? [])

	connect: (port, host='localhost') ->
		go = (err, addr) =>
			throw new Error("couldn't resolve: " + err) if err
			console.log "Resolved #{host} to #{addr}"
			options = onEvent: @_onEvent
			chrome.experimental.socket.create 'tcp', options, (si) =>
				@socketId = si.socketId

	end: -> @destroy() # TODO: only half-close the socket

	_onEvent: (ev) =>
		@_active()
		switch ev.type
			when 'connectComplete'
				if ev.resultCode < 0
