4 is a difficult age, no? I am finding it incredibly frustrating at times too!
Here's what works for us: I set brief, clear expectations ahead of time (like 10-15 minutes before we head out the door to the appt), and I make sure he can repeat the rules back to me. We call this "following the rules". And I praise him like crazy for good behavior, and give him reminders of, "Oh, that's not following the rules" kind of statements. (for the record, I also learn to let a lot of things go - like if they're crawling on the floor at the dr's office, ew, but fine....wash hands when you get home, and baths before bed, etc) Then when we get home, following great behavior, we make another big deal out of it - we tell daddy, call grandma, etc, depending on the event.
If I have to, I will excuse myself from the doctor, and set him in a time out immediately. I might also ask him to sit outside the door (with it open) to remove his "audience" so that I can talk to the doctor w/o interruption.
I know that when I try to "control" my kids, it almost always backfires on me. But, when I give them two choices (both directed toward the result I am aiming for), it actually gives the "control" back to them. For example - You can wear your blue shoes, or your tennis shoes. You can walk there, or I can carry you. You can sit in the chair quietly, or you can sit in the hallway quietly. You can stay in this play area, or you can come back to the table and sit. And I follow it up with, "You have a choice". And on and on....yes, it's exhausting, but it (mostly) gets me the results I want without too much resistance from my boys. It puts the responsibility of their behavior on them, and shows them (natural) consequences when needed.
Maybe when things are calm, and you're just hanging out at home with nowhere to go, you could talk about good behavior, and what helps Mommy, and what the rules are when out of the house.
Good luck, Mama & hang in there!