fixed socket write to ensure all bytes are sent