add length checking to write packets