Since it's every time you try to join a server, make sure you got the patch installed. If that's the case, uninstall the game, reinstall with the same CD-Key...then create the same account name.
I could connect to servers fine, but my brother (which uses another winxp profile and another account) had this "invalid cd-key" message. I tried to help him and used this BF2_code.exe (or whatever) in the support folder, resulting in the fact that I can't play now as well.
1 start the installer
2 select lanuage (and when you get to the welcomescreen, do not do anything)
3 press start-run and type in %temp%
4 find the folder wich have the bf2patchstuff
5 doubble-leftl-click on the cd-key-checker (or whatever it's called) in the folder you just found
6 Problem solved!
If you still have problems, go to the forums on planetbattlefield.com, they have dozen of thread reagrding this problem, and other problems with new patch.