title: nodeJS TLS contains odd characters in data
body: <p>I am trying to send and receive data from a TLS-secured service using mutual certificate authentication. I am successfully connecting to the service and the response is that I am authorized. I send data to the server and then wire up a 'data' event... I know my data is sent accurately, because I get semi-accurate response data.</p>
  
  <p>The data contains odd characters at the beginning and end of each chunk (and sometimes in the middle).</p>
  
  <pre><code>HTTP/1.1 200 OK
  X-Powered-By: Servlet/2.5
  Set-Cookie: JSESSIONID=8f2a41305cbe859001f1d54e5e80; Path=/xds; Secure
  Content-Type: application/soap+xml;charset=utf-8
  Transfer-Encoding: chunked
  Date: Thu, 13 Feb 2014 01:53:14 GMT
  
  6c
  &lt;?xml version='1.0' encoding='UTF-8'?&gt;&lt;S:Envelope x.........
  2000
  &lt;ns4:AdhocQueryResponse xmlns:ns2="urn:oasis:names:.........
  1485
  8425c" objectType="urn:oasis:names:tc:ebxml-regrep:.........
  0
  </code></pre>
  
  <p>Here is the code I am using:</p>
  
  <pre><code>var tls = require('tls');
  var net = require('net');
  
  var receiveData = function(sock) {
      var response = '';
      sock.on('error', function(err) {
          console.log("Socket error: " + err);
      });
  
      sock.on('data', function(d){
          response += d.toString();
      });
  
      sock.on('end', function() {
          console.log("Done receiving data from via TLS: " + response.length);
          console.log(response);
      });
  };
  
  var sendData = function(sock) {
      console.log("Sending request to HIE");
  
      var req = 'POST ' + path + ' HTTP/1.1\r\n' +
          'Host: localhost\r\n' +
          'Content-Length: ' + data.length + '\r\n' +
          'Content-Type: application/soap+xml; charset=utf-8\r\n' +
          'SOAPAction: "http://www.w3.org/2003/05/soap-envelope"\r\n' +
          '\r\n';
      sock.write(req);
      sock.write(data);
  };
  
  var options = {
      port: port,
      host: host,
      key: fs.readFileSync(tls_key, encoding='ascii'),
      cert: fs.readFileSync(tls_cert, encoding='ascii'),
      ca: fs.readFileSync(tls_ca, encoding='ascii')
  };
  
  var sock = tls.connect(options, function() {
      var authorized = sock.authorized;
  
      if (authorized) {
          console.log("Successfully connected via TLS");
          sock.setKeepAlive(true);
          receiveData(sock);
          sendData(sock);
      } else {
          console.log("Unauthorized to connect via TLS: " + sock.authorizationError);
      }
  });
  </code></pre>
  
  <p>Does anyone know why I am getting these odd characters? I've tried setting the sock's setEncoding('utf8'), and that removed my need to .toString() the data. But, that didn't solve the problem of these odd characters.</p>
  
  <p>I figured maybe they were indicators of how much data was in the chunk... but, the "6c" is in the middle of the first chunk. So, I don't think that's it. What's more, is the third chunk has a 0, even though there is a LOT of data sent in that chunk.</p>
  
  <p>Please help!</p>