Go through your code line by line with a debugger. Check what you are receiving from the mail server. You may also print the messages you are sending and receving to STDOUT, this may help.

Tell us where the program stops receiving or/and what exactly the server responds to you?