I have been building an application and it has been running fine for weeks now, until today, when I started to experience the errors you described.
My application makes multiple database calls from included files, classes, functions, any place you like. I can't for the life of me work out the set of circumstances which makes this error occur, but I think the error was occuring on trying to open a 2nd database connection.
Anyway I stumbled across a solution: change your 'pg_connect' calls to 'pg_pconnect' calls.
If anyone can shed more light on why this was happening that would be appreciated, I don't like putting fixes in without knowing why I'm doing it.
First I figured the pg_pconnect fixed it because it would be using the first connection again, instead of trying to create a second. Then I realised I only put the pconnect call into the 2nd connection. Wierd.