now listening on close event instead of end, emit close event on error,too