node.js/ File.on close socket?

I use node.js, and I open a socket. I have the next code:

 var file = fs.createReadStream(file1);
     file.on("data", function (data) {
            socket.write("blabla");
     });
     file.on("close", function (close){
}

It gives me the next error:

{ [Error: This socket has been ended by the other party] code: 'EPIPE' }

If I put socket.write("blabla"); before file.on, It works well.

How can I write to my socket?