Some versions of Android have a WiFi power saving mode which can come already activated when you buy the phone or by consequence of an activation made by an app (in my case it was the latter).
This mode will disconnect the WiFi connection whenever it's not being used and reconnect it when you start using it again. I found that it does it quite frequently though.
But some connections have security settings that require you to manually log in to the connection every time you want to connect, in these cases I can assure you this little problem becomes a pain in the ass.
So what I found was that in order to disable it you should dial *#0011# then you hit the menu button and select WiFi. You will get a page with the connection details and there you'll find a label saying WiFi power saving mode and a button under it, turn this button off.
This did the trick for me, I hope it helps.