Accessing socket object after disconnect

The following is a simple Node.js tcp server.

var net = require('net');

var conn = [];

var server = net.createServer(function(client) {//'connection' listener
    var i = conn.push(client) - 1;
    //console.log('[conn',i,']', client.remoteAddress, ':', client.remotePort);
    console.log('[disc]', conn[i].remoteAddress, ':', conn[i].remotePort);

    client.on('end', function() {
        console.log('[disc]', conn[i].remoteAddress, ':', conn[i].remotePort);
    });

    client.on('data', function(msg) {
        console.log('[data]', msg.toString());
    })

    client.write('hello\r\n');
    //client.pipe(client);
});

server.listen(8080);

When a client connects, sends, or disconnects, it will print information about the client. However, only when the client disconnects, it prints undefined rather than the socket info. Example:

[conn] 127.0.0.1 : 52711
[data] 127.0.0.1 : 52711 world!
[disc] undefined : undefined

Why does this happen? Is this because when it is closed the socket is destroyed? I need to know the information regarding the closing socket.

A disconnected socket has no remote endpoint, hence no remote address or remote port. The socket isn't destroyed, it's just no longer connected. If you need to know the remote address and port after the socket is disconnected, you'll need to keep track of it yourself.