Make sure packets are written before closing