La Solea near Plaza Mayor was the best for real flamenco music, very intimate and special place. Try to get there before 11pm because it can get too full.

There are many cheap hostals around Sol - MadridMan has a list of the best of them. If you need something really cheaper, its a risk, but I imagine you could get away with sleeping outside. The Retiro is huge, and you could hide yourself away in it for some rest.

Suerte.