Yea, that's called Lucid dreaming. There's ways to improve your chances of doing it, don't remember any other than it happens mostly in the few minutes before you would wake up normally. Usually the act of realizing it's a dream just snaps you out rather than say... letting you fly away into space