Rosana, it's possible that Rex's legs "go to sleep," like humans' do, if the weight of his body is on one leg or the other for a long time. You could check with the vet the next time you go to the office, but it's probably nothing serious.
As for what he does while he sleeps, do you know that dogs can dream? Carina sometimes shakes and can even cry in her sleep, then she wakes up and is perfectly fine. I wish I knew what she's dreaming about
Please give Rex a big hug from me.