This is not guarnteed to write all bytes, it should handle that case.