var constants = require('constants'),
	util = require('util');

function ReplClient(handler) {
	var self = this;

	// create Titanium socket for client messages
	self.socket = Ti.Network.Socket.createTCP({
		host: constants.HOST,
		port: constants.PORT,

		// handle socket errors
		error: function(err) {

			// print error back to console
			self.write(util.error('socket error: ' + err.toString()));

			// close connection if still open
			if (self.state === Ti.Network.Socket.CONNECTED) {
				self.close();
			}
		},

		// handle initial connection
		connected: function() {

			// pump all readable data from socket
			Ti.Stream.pump(self.socket, function(e) {
				if (e.bytesProcessed === -1 || !e.buffer) {
					self.write(util.error('message socket error: empty buffer, try again'));
				} else {
					handler.call(self, e.buffer.toString());
				}
			}, 1024, true);

			// tell server we're ready by sending back the resources directory
			self.write(JSON.stringify({
				resourcesDir: Ti.Filesystem.resourcesDirectory
			}), { eom: false });
		}
	});

	// create Titanium socket for client files
	self.fileSocket = Ti.Network.Socket.createTCP({
		host: constants.HOST,
		port: constants.PORT_FILE,

		// handle socket errors
		error: function(err) {

			// print error back to console
			self.write(util.error('file socket error: ' + err.toString()));

			// close connection if still open
			if (self.state === Ti.Network.Socket.CONNECTED) {
				self.close();
			}
		},

		// handle initial connection
		connected: function() {

			// pump all readable data from socket
			Ti.Stream.pump(self.fileSocket, function(e) {
				if (e.bytesProcessed === -1 || !e.buffer) {
					self.write(util.error('socket error: empty buffer, try again'));
				} else {
					var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'test123.js');
					file.write(e.buffer.toBlob());
				}
			}, 1024, true);
		}
	});
}

ReplClient.prototype.write = function write(data, opts) {
	opts = opts || {};
	if (!opts.raw) {
		data = JSON.stringify({
			data: data,
			type: opts.type || 'return'
		}) + constants.EOM;
	}
	Ti.Stream.write(this.socket, Ti.createBuffer({ value: data }), function(){});
};

ReplClient.prototype.writeError = function writeError(data, opts) {
	opts = opts || {};
	opts.type = 'error';
	this.write(data, opts);
};

ReplClient.prototype.connect = function connect() {
	this.socket.connect();
	this.fileSocket.connect();
}

ReplClient.prototype.close = function() {
	this.socket.close();
	this.fileSocket.close();
};

module.exports = ReplClient;


@@ -339,39 +339,33 @@
 le%0A%09%09%09self.write
-(util.e
+E
 rror('socket err
@@ -379,33 +379,32 @@
  err.toString())
-)
 ;%0A%0A%09%09%09// close c
@@ -712,31 +712,25 @@
 %09%09self.write
-(util.e
+E
 rror('messag
@@ -762,33 +762,32 @@
 fer, try again')
-)
 ;%0A%09%09%09%09%7D else %7B%0A%09
@@ -957,21 +957,17 @@
 ingify(%7B
-%0A%09%09%09%09
+ 
 resource
@@ -976,59 +976,27 @@
 ir: 
-Ti.Filesystem.resourcesDirectory%0A%09%09%09%7D), %7B eom: fals
+RDIR %7D), %7B raw: tru
 e %7D)
@@ -1249,31 +1249,25 @@
 %09%09self.write
-(util.e
+E
 rror('file s
@@ -1298,17 +1298,16 @@
 tring())
-)
 ;%0A%0A%09%09%09//
@@ -1635,15 +1635,9 @@
 rite
-(util.e
+E
 rror
@@ -1677,17 +1677,16 @@
  again')
-)
 ;%0A%09%09%09%09%7D 

write refactor