A general solution of the H2 control problem is presented for linear systems described by rational transfer matrices, not necessarily proper or stable. The control system is considered in the standard configuration, which includes the synthesis model of the plant and the controller. The H2 control problem consists of internally stabilizing the control system while minimizing the H2 norm of its transfer function. The notion of internal stability is based on bounded-input bounded-output stability and means that subsystems defined by any pair of input and output signals within the control system all are bounded-input bounded-output stable. In this manner, the optimal control system is devoid of impulsive as well as non-decaying exponential modes. The solution proceeds in three steps. First, the set of all controllers that internally stabilize the control system is parameterized. Then the subset of the stabilizing controllers that achieve a finite value of the H2 norm of the control system transfer matrix is described, also in parametric form. Finally, the optimal controller is obtained by selecting the parameter that minimizes the norm. The existence of each set of controllers described above is established in terms of the given data. There are plants that cannot be internally stabilized; there are plants that can be internally stabilized but no stabilizing controller renders the H2 norm finite; still there are plants and internally stabilizing controllers that achieve a finite value of the norm among which, however, no one actually minimizes the norm. The mathematical tools applied are doubly coprime, proper stable factorizations of rational matrices. Based on this description of the plant, two synthesis algorithms are derived: the primal and the dual one. The symmetries thus obtained are interesting and useful. The construction of the optimal controller requires two specific operations with proper stable rational matrices, inner--outer factorization and proper stable projection. The solution obtained is general in the sense that no assumptions on the plant are made other than those requiring the outer factors to be square, which can always be achieved.