What is the difference between a townhouse and a duplex?

A duplex is a pair of connected dwellings. A townhouse is a dwelling that is connected to more than two. Both can be two or more stories high.