 			if crlf?
 				line = @data.slice(0, crlf-1)
 				@data = @data.slice(crlf+1)
-				console.log '<=', line.toString('utf8')
-				@onCommand(parseCommand line)
+				fromSocketData line, (lineStr) =>
+					console.log '<=', lineStr
+					@onCommand(parseCommand lineStr)
 			else
 				break
 
 	send: (args...) ->
 		msg = makeCommand args...
 		console.log('=>', msg[0...msg.length-2])
-		@socket.write(msg)
+		toSocketData msg, (arr) => @socket.write arr
 
 	onCommand: (cmd) ->
 		switch cmd.command

		@send 'USER', @opts.nick, '0', '*', 'An irc5 user'

	onData: (pdata) ->
		@data += pdata
		while @data.length > 0
			cr = false
			crlf = undefined
			for d,i in @data
				d = d.charCodeAt 0
				if d == 0x0d
					cr = true
				else if cr and d == 0x0a

	chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	(chars[Math.floor(Math.random() * chars.length)] for x in [0...length]).join('')

# Many thanks to Dennis for his StackOverflow answer: http://goo.gl/UDanx
string2ArrayBuffer = (string, callback) ->
  bb = new WebKitBlobBuilder()
  bb.append(string)
  f = new FileReader()
  f.onload = (e) ->
    callback(e.target.result)
  f.readAsArrayBuffer(bb.getBlob())

arrayBuffer2String = (buf, callback) ->
  bb = new WebKitBlobBuilder()
  bb.append(buf)
  f = new FileReader()
  f.onload = (e) ->
    callback(e.target.result)
  f.readAsText(bb.getBlob())

arrayBufferToArrayOfLongs = (arrayBuffer) ->
  longs = []
  arrayBufferView = new Uint8Array(arrayBuffer)
  for i in [0...arrayBufferView.length]
    longs[i] = arrayBufferView[i]
  longs

arrayOfLongsToArrayBuffer = (longs) ->
	ab = new ArrayBuffer longs.length
	abView = new Uint8Array ab
	for i in [0...longs.length]
		abView[i] = longs[i]
	ab

toSocketData = (str, cb) ->
	string2ArrayBuffer str, (ab) ->
		cb arrayBufferToArrayOfLongs ab

fromSocketData = (arr, cb) ->
	ab = arrayOfLongsToArrayBuffer arr
	arrayBuffer2String ab, cb

class EventEmitter
	on: (ev, cb) ->
		@_listeners ?= {}
