See if this helps

the comments about reading the full packet is the key, i hadnt  realised that if you read when there is not data it dosent block.... the other changes might not be what you want! so have a play, not used this before so sorry for the waffle!