Problems with nodejs Buffer()

With the Buffer() I form the packet header to 100 bytes

According to the rules of formation of the title I have to specify the following:

Offset  Length(bytes)   Type    Description
0       4               Int     The length of the message (no header)
4       4               Int     Time to create the query (the number of seconds since January 1, 1970 GMT)
8       4               Int     The message ID
12      32                      Reserved (filled with null byte)
44      2               Int     The client ID
46      1                       1st byte of message flags
47      1                       2nd byte of message flags
48      4               Int     The identifier of the symmetric key
52      48                      Reserved (filled with null byte)

Here is my code:

var query="<?xml version=\"1.0\" encoding=\"utf-8\"?><sirena><query><get_currency_rates><curr1>RUB</curr1><curr2>USD</curr2><owner>IATA</owner></get_currency_rates></query></sirena>";

        var buf=new Buffer(100);
        var query1=new Buffer(query);
        console.log(query1.length);
        buf.writeInt32BE(query1.length, 0, true);//Длина текста сообщения (без заголовка)

        var foo = new Date;
        var unixtime_ms = foo.getTime();
        var unixtime = parseInt(unixtime_ms / 1000);
        console.log(unixtime);
        buf.writeInt32BE(unixtime, 4, true);//Время создания запроса (кол-во секунд с 1 января 1970 GMT)

        buf.writeInt32BE(1, 8, true);//id сообщения. потом нужно автоинкрементить

        for(var i=12; i<44;i++){//Зарезервировано (заполнено нулевым байтом)
            buf.writeInt8(0, i, true);
        }


        buf.writeInt16BE(5985, 44, true);//Идентификатор клиента

        buf.writeInt8(0, 46, true);//1-й байт флагов сообщения - обязательно ли это??
        buf.writeInt8(0, 47, true);//2-й байт флагов сообщения - обязательно ли это??

        buf.writeInt32BE(0, 48, true);//Идентификатор симметричного ключа - обязательно ли это??


        for(var i=52; i<100;i++){//Зарезервировано (заполнено нулевым байтом)
            buf.writeInt8(0, i, true);
        }

        var packet=buf.toString();//+query;

here is the byte order that gives buf.inspect()

<Buffer 00 00 00 a6 51 c0 15 db 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>

but it is passed to the socket

0000 00ef bfbd 51ef bfbd 15ef bfbd 0000
0001 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 1761 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 

what's happened?

you are using different encodings in conversions string<->buffer

//...
console.log(buf);
var packet=buf.toString('binary');//+query;
console.log(Buffer(packet, 'binary'));
//...

->

<Buffer 00 00 00 a6 51 c0 27 d6 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 ...>
<Buffer 00 00 00 a6 51 c0 27 d6 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 ...>

but

//...
console.log(buf);
var packet=buf.toString();//+query; // looks like utf8 here
console.log(Buffer(packet, 'binary')); // and binary here (even without explicit 'binary')
//...

->

<Buffer 00 00 00 a6 51 c0 27 d6 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 ...>
<Buffer 00 00 00 fd 51 fd 28 28 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 ...>