I think TP, Badblood, and 2tone have it right here.
I play until I feel I am not playing at my optimal level, and then I quit. I've had nights at the 2/4 tables where I've taken a $100 buy-in down to $18 and ended up making money in the session after a comeback.
Unfortunately, you "realizing" the fact that your not playing at your optimal level doesn't occur the first bad hand you play. So, I still lose a little money before I quit usually, but I'm prety good at figuring out sooner rather than later.
For a number of reasons, I've always had a lot of self-discipline. When I get knocked off my game, it's because I'm tired and can't focus anymore. Losing money doesn't change my play at all. To me, it's easy and natural. I guess it never occured to me how much of an obstacle this is to some players.