  We show that S-duality in four dimensional non-supersymmetric abelian gauge
theories can be formulated as a canonical transformation in the phase space of
the theory. This transformation is the usual interchange between electric and
magnetic degrees of freedom. It is shown that in phase space the modular
anomaly emerges as the result of integrating out the momenta degrees of
freedom. The generalization to d dimensional abelian gauge theories of p forms
is also considered. In the case of non-abelian gauge theories a careful
analysis of the constraints implied by the canonical transformation shows that
it does not relate Yang-Mills theories with inverted couplings. In fact the
dual theory is shown to be of Freedman- Townsend's type, also with ${\tilde
\tau}=-1/\tau$, $\tau=\frac{\theta}{2\pi}+\frac{4\pi i}{g^2}$.
