Added if test for !res.headersSent around res.writeHeaders method call o...

...n line 678 in gateway.js to prevent "headers sent" uncaught exceptions, i.e. when 504 timeout error response is already sent.