net = require('net');

server = net.createServer(function (socket) {

    console.log("Connection from: " + socket.remoteAddress);

    var testIndex = 0;

    var tests = ["1 + 1", "2 + 2", "3 - 4", "4 * 4", "9 / 0"];
    var results = ["2" , "4", "-1" , "16", "ERROR"];

    var client_md5 = "";

    socket.on('data', function (data) {
       if(data.toString().startsWith("HELLO"))
       {
            var message = data.toString().substring(0, data.toString().indexOf("\n") + 1);
            console.log("HELLO message: "+ message);
            client_md5 = message.substring(5, data.toString().indexOf("\n") - 1);
            console.log("Client MD5: " + client_md5);
            sendTest(socket, testIndex);

       }
       else if(data.toString().startsWith("RESULT"))
       {
            if(client_md5.length == 0)
            {
                socket.end();
                return;
            }
            var message = data.toString().substring(0, data.toString().indexOf("\n") + 1);
            console.log("RESULT message: " + message);
            var result = message.substring(7, data.toString().indexOf("\n") -1);
            console.log("Result of test: " + tests[testIndex]);
            console.log("Expected: " + results[testIndex]);
            console.log("Obtained: " + result);
            if(result == results[testIndex]) {
                console.log("Correct result");
            } else {
                console.error("Incorrect result");
                socket.end();
                return;
            }
            if(++testIndex < tests.length)
            {
                sendTest(socket, testIndex);
            }
            else
            {
                sendBye(socket);
            }
       }
       else
       {
           socket.end();
       }
    });

    function sendTest(client, index) {
        console.log("Sending test number: " + index);
        client.write("SOLVE " + tests[index] + "\n");
    }

    function sendBye(client) {
        client.write("BYE 33e87c9cfc6fbb5a57effa090404dd19\n");
        client.end();
    }
});

server.listen(55555);

@@ -209,16 +209,87 @@
  %229 / 0%22
+, %221000 / 2%22, %222 / 2%22, %22432749865987634387946482736147362498712634 / 2%22
 %5D;%0A    v
@@ -325,16 +325,37 @@
  , %2216%22,
+ %22ERROR%22, %22500%22, %221%22,
  %22ERROR%22
@@ -703,12 +703,8 @@
 %5Cn%22)
- - 1
 );%0A 
@@ -982,32 +982,84 @@
 ;%0A            %7D%0A
+%09    console.log(%22------------------------------%22);%0A
             var 
@@ -1180,32 +1180,32 @@
 : %22 + message);%0A
+
             var 
@@ -1267,11 +1267,8 @@
 %5Cn%22)
- -1
 );%0A 

Refactor logs