you should contact your host to determine why the connection works with the www domain but not without. this isn't a code problem but a server setup problem, the host will be able to help with that.
the only suggestion I would be able to makes to use mod rewrite to force the www address:
http://www.htaccessbasics.com/force-www-nonwww-domain/