var forge=require("./forge");require("./tls"),forge.tls.wrapSocket=function(e){var t=e.socket,n={id:t.id,connected:t.connected||function(e){},closed:t.closed||function(e){},data:t.data||function(e){},error:t.error||function(e){}},a=forge.tls.createConnection({server:!1,sessionId:e.sessionId||null,caStore:e.caStore||[],sessionCache:e.sessionCache||null,cipherSuites:e.cipherSuites||null,virtualHost:e.virtualHost,verify:e.verify,getCertificate:e.getCertificate,getPrivateKey:e.getPrivateKey,getSignature:e.getSignature,deflate:e.deflate,inflate:e.inflate,connected:function(e){1===e.handshakes&&n.connected({id:t.id,type:"connect",bytesAvailable:e.data.length()})},tlsDataReady:function(e){return t.send(e.tlsData.getBytes())},dataReady:function(e){n.data({id:t.id,type:"socketData",bytesAvailable:e.data.length()})},closed:function(e){t.close()},error:function(e,a){n.error({id:t.id,type:"tlsError",message:a.message,bytesAvailable:0,error:a}),t.close()}});t.connected=function(t){a.handshake(e.sessionId)},t.closed=function(e){a.open&&a.handshaking&&n.error({id:t.id,type:"ioError",message:"Connection closed during handshake.",bytesAvailable:0}),a.close(),n.closed({id:t.id,type:"close",bytesAvailable:0})},t.error=function(e){n.error({id:t.id,type:e.type,message:e.message,bytesAvailable:0}),a.close()};var i=0;return t.data=function(e){if(a.open){if(e.bytesAvailable>=i){var n=Math.max(e.bytesAvailable,i),s=t.receive(n);null!==s&&(i=a.process(s))}}else t.receive(e.bytesAvailable)},n.destroy=function(){t.destroy()},n.setSessionCache=function(e){a.sessionCache=tls.createSessionCache(e)},n.connect=function(e){t.connect(e)},n.close=function(){a.close()},n.isConnected=function(){return a.isConnected&&t.isConnected()},n.send=function(e){return a.prepare(e)},n.receive=function(e){return a.data.getBytes(e)},n.bytesAvailable=function(){return a.data.length()},n};