The Gamespy servers are total crap, I often have problems myself. I assume your firewall is letting the program through? Also in the in-game settings check the Network tab and make sure the IP address listed is your online IP address. Also try this:
Reset the Gamespy routing info for the game.
* Hold the windows key on your keyboard and press the r button.
* In the run dialog box, type %APPDATA% and press enter.
* This works for both – XP and Vista.
Go into the game folder.
When in that folder you'll see the Options.ini file, it may be in another subfolder, open it.
Search for:
GameSpyIPAddress = xxx.xxx.xxx.xxx (naturally there are other numbers)
To reset the routing for the game simply remove the whole line. [Remember to back it up!]
Save and exit the file and try to play online.