text: var util=require("util"),net=require("net"),tls=require("tls"),url=require("url"),driver=require("websocket-driver"),API=require("./api"),Event=require("./api/event"),DEFAULT_PORTS={"http:":80,"https:":443,"ws:":80,"wss:":443},SECURE_PROTOCOLS=["https:","wss:"],Client=function(e,n,t){t=t||{},this.url=e,this._driver=driver.client(this.url,{maxLength:t.maxLength,protocols:n}),["open","error"].forEach(function(e){this._driver.on(e,function(){c.headers=c._driver.headers,c.statusCode=c._driver.statusCode})},this);var r=t.proxy||{},i=url.parse(r.origin||this.url),a=i.port||DEFAULT_PORTS[i.protocol],o=SECURE_PROTOCOLS.indexOf(i.protocol)>=0,s=function(){c._onConnect()},u=t.tls||{},l=r.origin?r.tls||{}:u,c=this;u.ca=u.ca||t.ca,this._stream=o?tls.connect(a,i.hostname,l,s):net.connect(a,i.hostname,s),r.origin&&this._configureProxy(r,u),API.call(this,t)};util.inherits(Client,API),Client.prototype._onConnect=function(){var e=this._proxy||this._driver;e.start()},Client.prototype._configureProxy=function(e,n){var a,t=url.parse(this.url),r=SECURE_PROTOCOLS.indexOf(t.protocol)>=0,i=this;if(this._proxy=this._driver.proxy(e.origin),e.headers)for(a in e.headers)this._proxy.setHeader(a,e.headers[a]);this._proxy.pipe(this._stream,{end:!1}),this._stream.pipe(this._proxy),this._proxy.on("connect",function(){if(r){var e={socket:i._stream,servername:t.hostname};for(a in n)e[a]=n[a];i._stream=tls.connect(e),i._configureStream()}i._driver.io.pipe(i._stream),i._stream.pipe(i._driver.io),i._driver.start()}),this._proxy.on("error",function(e){i._driver.emit("error",e)})},module.exports=Client;