pg doesn't seem to close the connection correctly

I am writing an application using Node.js and postgresql. When a user authenticates it hits the DB to check the user's password then supposedly closes the connection to the DB. However, if the user enters the wrong password and tries again to authenticate, I get this error on the server console:

UNCAUGHT EXCEPTION...
{ [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', syscall: 'write' }
Error: write EPIPE

I believe that it is not closing the connection correctly the first time. This is the part of the script that is supposed to close the connection:

query.on('end', function(){
    client.end();
    if (pass == this.dbpass){
        console.log(pass);
        console.log(this.dbpass);
        return callback (200, "OK", {}, true);
    } else {
        console.log(pass);
        return callback(200, "OK", {}, false);
    }
});

Is there another way to close the connection that I am unaware of?