/**
 * A FreeDOM interface to Chrome sockets
 * TODO(willscott): Refactor into freedom-chrome.
 * @constructor
 * @private
 */
var Socket_chrome = function(channel) {
  this.appChannel = channel;
  this.sid = null;
  this.create = chrome.socket.create;
  this.connect = chrome.socket.connect;
  this.read = chrome.socket.read;
  this.write = chrome.socket.write;
  this.listen = chrome.socket.listen;
  this.accept = chrome.socket.accept;
  this.getInfo = chrome.socket.getInfo;
};

Socket_chrome.prototype.destroy = function(socketId, continuation) {
  if (chrome && chrome.socket) {
    chrome.socket.destroy(socketId);
  }
  continuation();
};

Socket_chrome.prototype.disconnect = function(socketId, continuation) {
  if (chrome && chrome.socket) {
    chrome.socket.disconnect(socketId);
  }
  continuation();
};

@@ -257,225 +257,1616 @@
 his.
-connect = chrome.socket.connect;%0A  this.read = chrome.socket.read;%0A  this.write = chrome.socket.write;%0A  this.listen = chrome.socket.listen;%0A  this.accept = chrome.socket.accept;%0A  this.getInfo = chrome.socket.getInfo
+write = chrome.socket.write;%0A  this.getInfo = chrome.socket.getInfo;%0A%7D;%0A%0A// Error codes can be found at:%0A// https://code.google.com/p/chromium/codesearch#chromium/src/net/base/net_error_list.h%0A%0ASocket_chrome.prototype.connect = function(socketId, hostname, port, callback) %7B%0A  chrome.socket.connect(socketId, hostname, port, function connectCallback(result) %7B%0A    callback(result);%0A    var dataRead = function (readInfo) %7B%0A      if (readInfo.resultCode === 0) %7B%0A%09this.dispatchEvent('onData', %7BsocketId: socketId, data: readinfo.data%7D);%0A%09readLoop();%0A      %7D  else if (readInfo.resultCode !== -15) %7B%0A%09// The result code is -15 if the connection was closed, which can%0A%09// can happen in usual program flow, so we will not log the error.%0A%09console.error('Error occured when reading from socket ' + socketId);%0A      %7D;%0A    %7D;%0A    var readLoop = function () %7B%0A      chrome.socket.read(socketId, dataRead);%0A    %7D;%0A    readLoop();%0A  %7D);%0A%7D;%0A%0ASocket_chrome.prototype.listen = function(socketId, address, port, callback) %7B%0A  chrome.socket.listen(socketId, address, port, function listenCallback(result) %7B%0A    callback(result);%0A    if (result === 0) %7B%0A      var acceptCallback = function (acceptInfo) %7B%0A%09if (acceptInfo.resutCode === 0) %7B%0A%09  this.dispatchEvent('onConnection', %5BsocketId, acceptInfo.socketId%5D);%0A%09  acceptLoop();%0A%09%7D else if (readInfo.resultCode !== -15) %7B%0A%09  console.error('Error ' + acceptInfo.resultCode%0A%09%09%09+ ' while trying to accept connection on socket ' + socketId);%0A%09%7D%0A      %7D;%0A      var acceptLoop = function() %7B%0A%09chrome.socket.accept(socketId, acceptCallback);%0A      %7D;%0A      acceptLoop();%0A    %7D%0A  %7D)
 ;%0A%7D;

Implement new freedom socket API in socket_chrome