将php代码迁移到nodejs(套接字)

I need to migrate PHP code to Nodejs. This PHP code makes a connection to another server using socket, writes a file and then takes the result of another file.

The code is as follows: (it is a summary example of my code)

$compilerhost = "xxx.xxx.x.x"
$compilerport = 1111;

$socket = fsockopen($compilerhost, $compilerport); //connect to server via socket

if($socket)
{
    fwrite($socket, "data1"."
"); // write file line 1

    fwrite($socket, "data2"."
"); // write file line 2

    $result = fgets($socket); // get results

    echo $result;
}

My code in Nodejs:

var fs = require('fs');
var net = require('net');
var socket = new net.Socket();

socket.connect (port, host, function() {
    console.log('CONNECTED TO: ' + compilerHost + ':' + compilerPort);

    fs.writeFile(socket, "test", function(err) {
        if(err) {
            console.log(err);
        }
    });                     
});