Synchronize PacketWriter shutdown

try the best to send the queue elements and the closing stream element.