This kind of problems usually appear on some network programs.
I suggest that you'd better write a class called "logger" which logs input/ouput information to a file, and when error occurs, just check the file to find the problem.
And also, you could write your own tools to check the log file automatically.