Neither can I understand how to do better, I decided to write here.
You need to make sure that the InputStream receives data in the background. Tobish if the data is there, it calculates them and waits further. Does not close !.
If there is no data, it just waits, if the data is there, it accepts it and waits further.
How to do this?