Fix: emit write stream "end" after closing the stream
* This fixes a race condition where the stream may still be open
  after end is emitted and handled
Merge pull request #507 from luvit/fixes/stream_end_ordering

Fix: emit write stream "end" after closing the stream