Make sure that imap is enabled in gmail: http://mail.google.com/support/bin/answer.py?answer=77695
After looking at a blog post turns out there is one significant problem for connecting. The firewall. If the port is blocked, then you can't make a connection. I will try to see with an admin if...