Buffer messages when socket is closed and improve retries