I solved my problem!
in the line
int code = Integer.parseInt(reply.substring(0,3));
That has to be what's throwing the NumberFormat exception. I had to put a try block in there and return -1 if an exception is indeed thrown.
output the substring before you try to parse it so you can see exactly what it's trying to parse.