Your second attempt has the problem, that you try and process the data before you might have received all of it.