title: Different reaction of a server for (almost) the same packets
body: <p>I have a small script in Node.js (which is a specific case of a bigger application), which connects to a certain service and then sends periodically simple JSON command. Requests are sent "synchronously", so the next request is sent after receiving the last response.</p>
  
  <p>Unfortunately, sometimes I do not receive the answer (TCP PUSH is sent to the server and my script receives ACK, but there is no PUSH from the server). Therefore, the script hangs. It would suggest a bug in the server, however, the same script has been written also in Python and it works perfectly - all requests receive responses (and the Pythonic script has been working for over 17 hours, whereas the one from Node.js hangs differently, sometimes between 15 minutes and 2 hours).</p>
  
  <p>The communication is encrypted using TLS (tls module in node and ssl in python). The only observed difference lies in the sizes of SSL packets - req/res pair in Node weighs 53/77 bytes, whereas in Pythonic script it has 90/85 bytes. </p>
  
  <p>The server is written in C#, however, I do not have any more information, nor access to it (private key is also unreachable - I would check what gets transmitted then).</p>
  
  <p>I have also tested the situation, in which I do not wait for the response in Node (I just used setInterval on small function with call to write()). Sometimes there are two-three requests sent consecutively and only then I get the responses. However, some of them are malformed (instead of content I receive only the message separator - that would suggest problem on the server side, however, this does not show up in Pythonic version).</p>
  
  <p>What would you suggest? May this problem be related to SSL/TLS? Something with the .NET? Or maybe Node? Thanks in advance for any suggestions.</p>
  
  <p>Code in Node.js:</p>
  
  <pre><code>const tls = require('tls');
  var carrier = require('carrier');
  
  var client = tls.connect(1111, 'host', {rejectUnauthorized: false}, function() {
      var reqNum = 0;
      var resCarrierNum = -1; // because there is additional login message
      var resClientNum = -1;
      client.on('data', function(data) {
          resClientNum++;
          setTimeout(function() {
              client.write('{"command":"getVersion"}');
              reqNum++;
          }, 400);
      });
      carrier.carry(client, function(data) {
          console.log(data);
          var response = JSON.parse(data);
          if(!response.status) {
              console.log('ERROR' + response.errorCode + ': ' + (response.errorDescr));
          }
          resCarrierNum++;
      }, "utf8", /\n\n/);
      client.write(JSON.stringify({
          "command" : "login",
          "arguments" : {
              "userId" :  1,
              "password" : ''
          }
      }));
  
  });
  </code></pre>
  
  <p>Code in Python:</p>
  
  <pre><code>import socket
  import ssl
  import sys
  import time
  
  addr = sys.argv[1]
  port = int(sys.argv[2])
  delay = int(sys.argv[3])
  
  def getDateOfBuild(sslS, counter):
  
      print 'Try', counter, time.asctime()
      sslS.send('{"command":"getVersion"}')
  
      sf = sslS.makefile()
      while True:
          line = sf.readline()
          if line == '\n':
              break;
          if line == '':
              print "ERROR: connection closed,", time.asctime()
              sys.exit()
          print line
  
  s = socket.socket()
  sslS = ssl.wrap_socket(s)
  sslS.connect((addr, port))
  print 'Connected from', sslS.getsockname()
  
  print repr(sslS.getpeername())
  counter = 0
  sslS.write('{\"command\":\"login\",\"arguments\":{\"userId\":\"\",\"password\":\"\"}}\n')
  
  while True:
      getDateOfBuild(sslS, counter)
      counter += 1
      time.sleep(0.4)
  </code></pre>
  
  <p>There is a small difference in the command, but versions with the same exact string have been tested, with the same results.</p>