I have a really annoying problem!

I'm a musician, and I have been for years, and very often right before I fall a sleep, melodies pop in to my head. And they're really good. The problem is that the second I open my eyes, the melody is completely gone. They appear when I'm in a state between dream and reality.

I remember all my thoughts and images during the "dream" but never the melodies!

Anyone has any ideas of how I could start remembering these melodies?